jQuery .ajax() does not work in Safari when it takes seconds to get returned data

后端 未结 2 1926
花落未央
花落未央 2021-02-10 09:40

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

相关标签:
2条回答
  • 2021-02-10 10:03

    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.

    0 讨论(0)
  • 2021-02-10 10:15

    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.

    0 讨论(0)
提交回复
热议问题