跨域的 jQuery AJAX 对于 Chrome 等工作正常,但不能在 IE10 中发送数据,也不能在 IE <= 9 中发送请求
Posted
技术标签:
【中文标题】跨域的 jQuery AJAX 对于 Chrome 等工作正常,但不能在 IE10 中发送数据,也不能在 IE <= 9 中发送请求【英文标题】:jQuery AJAX over cross domain working fine for Chrome etc but not sending data in IE10 and not sending request in IE <= 9 【发布时间】:2014-04-21 11:09:33 【问题描述】:在我的 Web 应用程序中,我需要将数据发送到托管在不同域上的 API,API 处理接收到的数据,然后 jQuery AJAX 处理 API 的响应。
此调用在 Chrome 和 Firefox 上运行良好,但不适用于 IE。 AJAX 正在调用 IE10 中的 API,但调用时未发送任何数据。我添加了 put_file_contents(fileName, receivedData) 来检查 API 是否接收到任何数据。正在创建文件,但它是空白的。 AJAX 甚至没有为 IE
代码:
在 AJAX 调用之前添加了此脚本以支持 IE 上的 CORS - https://github.com/MoonScript/jQuery-ajaxTransport-XDomainRequest/blob/master/jquery.xdomainrequest.min.js
jQuery(document).ready(function ()
jQuery.support.cors = true;
var link = new String(window.location) + "";
jQuery.ajax(
url: 'URL_OF_API',
type: 'post',
dataType: 'json',
data:
'variable1' : 'value1',
'variable2' : 'value2'
,
success: function (data)
//work on received data
,
error: function ()
//This is executed in case of IE<=9
);
);
请帮忙。提前致谢!
【问题讨论】:
【参考方案1】:使用IE developer tool 进行调试。
如果您收到如下错误消息:
... Request method post was not present in the Access-Control-Allow-Methods list
分析您的响应标头。您应该会看到如下内容:
Access-Control-Allow-Origin: http://www.yourdomain.com
Access-Control-Allow-Methods: POST
您在哪个域上测试?
也许这些链接可以帮助你更多:
Using CORS for Cross-Domain Ajax Requests CORS for XHR in IE10 IE9 jQuery AJAX with CORS returns “Access is denied”【讨论】:
以上是关于跨域的 jQuery AJAX 对于 Chrome 等工作正常,但不能在 IE10 中发送数据,也不能在 IE <= 9 中发送请求的主要内容,如果未能解决你的问题,请参考以下文章