$.ajax (jQuery v1.9.1) 在 iPad 上不起作用

$.ajax (jQuery v1.9.1) not work on iPad

本文关键字:iPad 不起作用 ajax jQuery v1      更新时间:2023-09-26

Ajax 不仅在 iPad 上运行

$.ajax({
    url         : 'http://www.othersite.com/GetUrl?Callback=?',
    dataType    : 'json',
    contentType : 'application/json; charset=utf-8',
    type        : 'GET',
    timeout     : 5000,
    cache       : false,
    crossDomain : true,
    async       : true,
    data        : { id : 100, noCache : new Date().getTime() },
    beforeSend  : function(XMLHttpRequest) { alert('Sended!'); },
    complete    : function(XMLHttpRequest, textStatus) { alert('Completed with status: ' + textStatus) },
    success     : function(data){ alert('Success with response:' + data.response ); },
    error       : function(xhr, textStatus, errorThrown){ alert('Error: ' + textStatus); }
});

iPad 上的提醒

Sended!
Completed with status: timeout;
Error: timeout

IE (7,8,9,10,11), Firefox, Chrome, Opera, Safari 桌面中的警报

Sended!
Completed with status: success
Success with response: http://www.google.com/

服务器应用(平均响应时间为 50 毫秒)

echo $_GET['Callback'].'('.json_encode(array('response' => 'http://www.google.com/')).')';

服务器响应标头

Access-Control-Allow-Origin: *
Cache-Control: no-cache
Connection: close
Content-Length: 152
Content-Type: application/json; charset=utf-8
Date: Tue, 02 Dec 2014 09:52:46 GMT
Pragma: no-cache

服务器响应正文

jQuery19109435868220625793_1417512417785({"response": "http://www.google.com/"})

在服务器日志中,有来自iPad的请求,但显示来自其他浏览器的所有请求。

为什么会这样?

Safari 会

忽略同步 AJAX 调用中的超时设置。如果您的页面在 10 秒内未收到来自服务器的响应,则会收到NETWORK_ERR错误。

尝试在 AJAX 调用中添加特定的timeout属性

timeout: 240000

更新

似乎不可能更改 10 秒作为最大超时,因为 Safari 认为更长的等待对用户来说是"不好的"。您可以异步调用并使用承诺来模拟同步