带有 CORS 的 IE9 jQuery AJAX 返回“访问被拒绝" [英] IE9 jQuery AJAX with CORS returns "Access is denied"

查看:29
本文介绍了带有 CORS 的 IE9 jQuery AJAX 返回“访问被拒绝"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下适用于所有浏览器除了 IE(我正在 IE 9 中测试).

jQuery.support.cors = true;...$.ajax(url + "消息/postMessageReadByPersonEmail",{跨域:是的,数据: {messageId : messageId,个人电子邮件:个人电子邮件},成功:函数(){alert('标记为已读');},错误:函数(a,b,c){警报(失败");},类型:'发布'});

我有另一个使用 dataType: 'jsonp' 的函数,但我不需要在此 AJAX 调用中返回任何数据.我最后的手段是返回一些用 JSONP 包裹的乱码,以使其正常工作.

知道为什么 IE 会搞砸一个不返回数据的 CORS 请求吗?

解决方案

这是一个已知的bug 使用 jQuery.jQuery 团队没有计划在核心中支持这一点,并且更适合作为插件."(参见此评论).IE 确实使用XMLHttpRequest,而是使用一个名为XDomainRequest的替代对象.

一个插件可以在 jQuery 中支持这个,可以在这里找到:https://github.com/jaubourg/ajaxHooks/blob/master/src/xdr.js

编辑$.ajaxTransport 函数注册了一个传输器工厂.$.ajax内部 使用传输器来执行请求.因此,我假设您应该能够像往常一样调用 $.ajax.可以在这里找到有关传输器和扩展 $.ajax 的信息.

此外,可以在此处找到此插件的更好版本..p>

另外两点:

  1. XDomainRequest 对象是 从 IE8 引入 并且在以下版本中不起作用.
  2. 从 IE10 CORS 将是 支持使用普通的 XMLHttpRequest.

编辑 2:http 到 https 问题

<块引用>

请求必须针对与托管页面相同的方案

此限制意味着如果您的 AJAX 页面位于http://example.com,那么您的目标 URL 也必须以 HTTP 开头.同样,如果您的 AJAX 页面位于 https://example.com,那么您的目标 URL 也必须以 HTTPS 开头.

来源:http://blogs.msdn.com/b/ieinternals/archive/2010/05/13/xdomainrequest-restrictions-limitations-and-workarounds.aspx

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'
            }
        );

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.

Any ideas why IE is screwing up with a CORS request that returns no data?

解决方案

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.

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 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.

Also, a perhaps better version of this plugin can be found here.

Two other notes:

  1. The object XDomainRequest was introduced from IE8 and will not work in versions below.
  2. From IE10 CORS will be supported using a normal XMLHttpRequest.

Edit 2: http to https problem

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.

Source: http://blogs.msdn.com/b/ieinternals/archive/2010/05/13/xdomainrequest-restrictions-limitations-and-workarounds.aspx

这篇关于带有 CORS 的 IE9 jQuery AJAX 返回“访问被拒绝"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆