以下适用于所有浏览器除了 IE(我正在 IE 9 中测试)。
jQuery.support.cors = true;
...
$.ajax(
url + "messages/postMessageReadByPersonEmail",
{
crossDomain: true,
data: {
messageId : messageId,
personEmail : personEmail
},
success: function() {
alert('marked as read');
},
error: function(a,b,c) {
alert('failed');
},
type: 'post'
}
);
我有另一个使用 dataType: 'jsonp'
的函数,但我不需要在此 AJAX 调用中返回任何数据。我最后的办法是返回一些包裹在 JSONP 中的乱码,以使其正常工作。
知道为什么 IE 搞砸了不返回任何数据的 CORS 请求吗?
最佳答案
这是一个已知的 bug使用 jQuery。 jQuery 团队“没有计划在核心中支持它,更适合作为插件”。 (参见 this comment)。 IE 不使用XMLHttpRequest,而是使用名为XDomainRequest 的替代对象。
在 jQuery 中有一个插件可以支持这个,can be found here : https://github.com/jaubourg/ajaxHooks/blob/master/src/xdr.js
编辑
$.ajaxTransport
函数注册了一个传输器工厂。 $.ajax
内部使用传输器来执行请求。因此,我假设您应该能够像往常一样调用$.ajax
。可以找到有关传输器和扩展 $.ajax
的信息 here .
此外,可以找到此插件的更好版本 here .
另外两个注意事项:
- 对象 XDomainRequest 是 introduced from IE8并且不适用于以下版本。
- 从 IE10 CORS 将是 supported using a normal XMLHttpRequest .
编辑 2:http 到 https 问题
Requests must be targeted to the same scheme as the hosting page
This restriction means that if your AJAX page is at http://example.com, then your target URL must also begin with HTTP. Similarly, if your AJAX page is at https://example.com, then your target URL must also begin with HTTPS.
关于jquery - 带有 CORS 的 IE9 jQuery AJAX 返回 "Access is denied",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10232017/