0

I'm trying to download a generated file(pdf) using Ajax, it's working perfectly in Chrome and IE but not in FireFox. Here is my code :

function Download(urlAction, urlDownload) {

    $.ajax({
        type: "post",
        url: urlAction,
        data: {
            'itemIds': checkedItems,
            'dateMin': datemin.toISOString(),
            'dateMax': datemax.toISOString()
        },
        datatype: "json",
        traditional: true,
        success: function (data) {
            console.log('fff', data);
            if (data.success) {
                window.location = urlDownload;
            }
        }
        error: function (xhr, textStatus, err) {
            console.log("readyState: " + xhr.readyState);
            console.log("responseText: " + xhr.responseText);
            console.log("status: " + xhr.status);
            console.log("text status: " + textStatus);
            console.log("error: " + err);
        }
    });
}
}

In the UrlAction I generate the file in Json format and post it in a session, then calling it again in my urlDownload. In Chrome and IE the file is downloaded without reloading the page, but in Firefox, it only reload the page.

The error says:

  • readyState: 0
  • responseText: undefined
  • status: 0
  • text status: error
  • error: undefined

2 Answers2

0

Jquery $.ajax says:

dataType (default: Intelligent Guess (xml, json, script, or html)) Type: String The type of data that you're expecting back from the server. If none is specified, jQuery will try to infer it based on the MIME type of the response

so you can try to remove the parameter first.

About your error status, it's 0, you could find detailed reason here: XMLHttpRequest status 0 (responseText is empty)

About your error handler,error: function (xhr, textStatus, err):

  1. xhr.status shows reason, you have to check your server side configuration; but you did not mentions what is server environment.
  2. textStatus could be following values:"timeout", "error", "abort", and "parsererror";
  3. err could be undefined if request did not reach to your server side location;

Here is official document:

Type: Function( jqXHR jqXHR, String textStatus, String errorThrown ) A function to be called if the request fails. The function receives three arguments: The jqXHR (in jQuery 1.4.x, XMLHttpRequest) object, a string describing the type of error that occurred and an optional exception object, if one occurred. Possible values for the second argument (besides null) are "timeout", "error", "abort", and "parsererror". When an HTTP error occurs, errorThrown receives the textual portion of the HTTP status, such as "Not Found" or "Internal Server Error." As of jQuery 1.5, the error setting can accept an array of functions. Each function will be called in turn. Note: This handler is not called for cross-domain script and cross-domain JSONP requests. This is an Ajax Event.

Dongdong
  • 2,208
  • 19
  • 28
0

The thing that worked for me is that I put the button (the one that calls this method) out of the form div, and it stoped reloading the page ,the download worked like a charm.