IE9具有CORS的jQuery AJAX返回“访问被拒绝” [英] IE9 jQuery AJAX with CORS returns "Access is denied"
问题描述
以下工作方式适用于所有浏览器,除了IE (我在IE 9中测试)。
The following works in all browsers except IE (I'm testing in 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中的乱码,只是为了使它工作。
I have another function which uses dataType: 'jsonp'
, but I don't need any data returned on this AJAX call. My last resort will be to return some jibberish wrapped in JSONP just to make it work.
任何想法为什么IE正在与一个CORS请求返回没有数据? / p>
Any ideas why IE is screwing up with a CORS request that returns no data?
推荐答案
这是一个已知的错误与jQuery。 jQuery团队没有计划在核心支持这一点,更适合作为插件。 (请参阅此评论)。
IE不是 使用 XMLHttpRequest ,而是一个名为 XDomainRequest 的替代对象。
This is a known bug with jQuery. The jQuery team has "no plans to support this in core and is better suited as a plugin." (See this comment). IE does not use the XMLHttpRequest, but an alternative object named XDomainRequest.
有 是可在jQuery中支持此插件的插件,可在此处找到:
https://github.com/ jaubourg / ajaxHooks / blob / master / src / xdr.js
There is a plugin available to support this in jQuery, which can be found here: https://github.com/jaubourg/ajaxHooks/blob/master/src/xdr.js
EDIT
函数 $。ajaxTransport
注册了一个传输工厂。通过 $。ajax
在内部使用转运器来执行请求。因此,我假设,您应该可以像往常一样调用 $。ajax
。有关运输商和扩展 $。ajax
的信息,请访问此处 。
EDIT
The function $.ajaxTransport
registers a transporter factory. A transporter is used internally by $.ajax
to perform requests. Therefore, I assume you should be able to call $.ajax
as usual. Information on transporters and extending $.ajax
can be found here.
此外,您可以在此处找到此插件的更好版本。
其他两个注意事项:
- 对象XDomainRequest 从IE8引入,不会工作在以下版本。
- 从IE10 CORS将使用正常的XMLHttpRequest 支持。
- The object XDomainRequest was introduced from IE8 and will not work in versions below.
- From IE10 CORS will be supported using a normal XMLHttpRequest.
编辑2:http到https问题
请求必须定位到与托管页面相同的方案
Requests must be targeted to the same scheme as the hosting page
此限制表示,如果您的AJAX网页位于
http://example.com ,则您的目标网址也必须以HTTP。
同样,如果您的AJAX网页位于 https://example.com ,则您的
目标网址也必须以HTTPS。
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.
这篇关于IE9具有CORS的jQuery AJAX返回“访问被拒绝”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!