jquery - 带有 CORS 的 IE9 jQuery AJAX 返回 "Access is denied"

标签 jquery internet-explorer-9 cross-domain jsonp cors

以下适用于所有浏览器除了 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 .

另外两个注意事项:

  1. 对象 XDomainRequest 是 introduced from IE8并且不适用于以下版本。
  2. 从 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.

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

关于jquery - 带有 CORS 的 IE9 jQuery AJAX 返回 "Access is denied",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10232017/

相关文章:

jquery - html 下拉子菜单在移动尺寸下无法正常运行

javascript - 为什么第一次只获取table的tr?

javascript - 为什么JS函数在IE9下不能正常工作?但在 Chrome 和 Firefox 中运行良好

html - <ul> 在 IE9 中不能正常工作?

javascript - 子域的谷歌分析代码跟踪器(跟踪两个页面)

javascript - 如何有条件地将外部 json 文件从 CDN 加载到我的 javascript 中?

javascript - 访问控制请求 header : - x-requested-with

javascript - 找出您刚刚移动的表格的哪一行

javascript - 在 AJAX 成功响应中执行 Javascript 代码(在单独的文件中)?

javascript - 侧边栏和标题在 IE9 上无法正确呈现