I'm using the jQuery ajax() method to pass on (GET) data to another ExportData page, and get the returned data ("succeeded/failed") after that page processes. The ExportData page takes about 10 seconds before it returns "succeeded/failed".
The code I'm using works well in other browsers (FF/IE/Chrome) but NOT in Safari. In Safari's code inspector the status code showed is -1001 and no data was received. Whilst there're other ajax() requests (not need so long as this page to return data) and they do work (with status of 200).
I had a guess that the long interval between sending and receiving data causes error for Safari. However I also found the -1001 error shows very quickly, that might means the ExportData.php was not executed.
Does anyone have an idea about this issue? Your help will be greatly appreciated!
The code is:
ExportData: function (date,view) {
ajaxcall.data = {};
ajaxcall.url = 'ExportData.php?Date='+date+'&View='+view;
ajaxcall.callbackfunction = UserEvents.ExportSuccess;
ajaxcall.Call();
},
The "ajaxcall" is in another file:
var ajaxcall = {
SitePath: '',
data: '',
url: '',
callbackfunction: '',
fileElementClass: '',
AjaxRequest: false,
callback: true,
Call: function () {
if (ajaxcall.AjaxRequest == true) {
alert(ajaxcall.AjaxRequest);
return;
}
else {
try {
ajaxcall.AjaxRequest == true;
$.ajax({
type: "GET",
url: ajaxcall.url,
data: ajaxcall.data,
dataType: "json",
async: false,
success: function (data) {
if (ajaxcall.callback == true) {
ajaxcall.callbackfunction(data);
}
},
error: function (request, status, error) {},
complete: function () {
ajaxcall.AjaxRequest = false;
}
});
}
catch (e) {
ajaxcall.AjaxRequest == false;
}
}
},
};
Thanks, Sean Liu
This is an old question, and I'm not sure if you have found the answer, but we recently ran into this same issue. After a lot of research and testing, we found out that Safari ignores timeout settings for Synchronous AJAX calls, limiting to 10 seconds. I guess this is considered a user experience thing, as synchronous calls will make the page appear to hang, and they may assume that doing so for a long period of time is either not user friendly or an unresponsive page.
So you basically have two options, you can either modify your code to use an output buffer to periodically send data back. This should trigger Safari to recognize that the request is at least active and responding, and usually won't force it to time out. We never tried this, so I can't vouch for it, but I have seen some stories of varying success here and there. The other option is to just change the AJAX call to be asynchronous. This is probably the easier method, but you may need to implement some promises to "simulate" a synchronous response if your application depends on having a synchronous call.
For reference, here's where we got started on tracking down the issue: http://propercode.com/wordpress/?p=32.
Try setting a timeout
in your settings object.
Set a timeout (in milliseconds) for the request. This will override any global timeout set with $.ajaxSetup(). The timeout period starts at the point the $.ajax call is made; if several other requests are in progress and the browser has no connections available, it is possible for a request to time out before it can be sent. In jQuery 1.4.x and below, the XMLHttpRequest object will be in an invalid state if the request times out; accessing any object members may throw an exception. In Firefox 3.0+ only, script and JSONP requests cannot be cancelled by a timeout; the script will run even if it arrives after the timeout period.
来源:https://stackoverflow.com/questions/12176795/jquery-ajax-does-not-work-in-safari-when-it-takes-seconds-to-get-returned-dat