jquery AJAX stopped working with iOS 5.0.1
以下逻辑用于在我们支持的所有移动设备上使用jquery 1.4.4和1.7.1:
1 2 3 4 5 6 7 8 9 10 11 12 | $.ajax({ url: 'http://www.example.com/someurl', type: 'GET', dataType: 'text', timeout: 60000, success: function(data) { alert(data); }, error: function(jqXHR) { alert(jqXHR.state()); } }); |
但是对于iOS 5.0.1,上面输入错误函数警告
这是一个已知的jquery / iOS 5.0问题吗? 我该怎么调试呢? 有没有解决方法? 我不知道从哪里开始寻找。
注意:我在服务器端注意到,发出
Mobile Safari on iOS 5.1 Unexpectedly Making Cross Origin Resource Sharing Requests
我在问题中提供的链接实际上是指向解决方案。我的一些ajax请求用于获取使用流式传输的PDF的URL
1 | Content-Disposition: attachment; filename="somename.pdf" |
显然,这会导致iOS 5.0的Safari出现重大问题,打破
Mobile Safari on iOS 5.1 Unexpectedly Making Cross Origin Resource Sharing Requests
谢谢你的调查。我有同样的问题。收到文件作为"附件"后,Mobile Safari会在调用时发送OPTIONS请求:
1 | $.ajax('/url') |
但是,如果我从同一个文件运行以下代码,它会生成一个有效的GET请求:
1 2 3 | http = new XMLHttpRequest() http.open("GET","/url") http.send() |
为什么jQuery不起作用但XMLHttpRequest在这里工作?
The reason for the error is the same origin policy. It only allows you
to do XMLHTTPRequests to your own domain. See if you can use JSON
instead.
参考资料和推荐资源;
https://stackoverflow.com/a/1109261/896341
http://api.jquery.com/jQuery.getJSON/
http://www.w3.org/Security/wiki/Same_Origin_Policy
https://developer.mozilla.org/En/Same_origin_policy_for_JavaScript