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

一个人想着一个人 提交于 2019-12-03 08:41:48

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!