【发布时间】:2012-10-03 14:56:18
【问题描述】:
我的 Ajax 跨域请求在 IE 9 中失败并显示“访问被拒绝”。我已经阅读了有关此主题的几篇文章,并且 AFAIK 它应该可以工作。
- IE9 和 jQuery 1.8.1
- 呼叫是
async、jsonp和crossdomain、cache是false。这些是我找到的先决条件。 - 适用于最新的 Firefox 和 Chrome。
-
jQuery.support.cors是真的 - 甚至设置了响应头:
Access-Control-Allow-Origin:*(SO) - 返回的 JSON 代码也是正确的,使用了检查器(另见 3。)
那么,为什么 Access denied 会失败呢?任何想法?可能是因为我的代码是从“JavaScript”库中调用的,而不是页面上的 <script></script> 标记?
我错过了什么?
// The code is part of an object's method (prototype)
// code resides in a library "Mylib.js"
$.ajax({
type: 'GET',
url: url,
cache: false,
async: true,
crossdomain: true, // typo, crossDomain, see my answer below
datatype: "jsonp", // dataType
success: function (data, status) {
if (status == "success" && !Object.isNullOrUndefined(data)) { ... }
},
error: function (xhr, textStatus, errorThrown) {
// access denied
}
});
--编辑--基于Robotsushi的cmets,一些进一步的研究---
- 确实在jQuery源码(1.8.1)中找不到
XDomainRequest - 如果我不设置 cors (
jQuery.support.cors = true),我最终会出现“无传输”异常。 - 仍然想知道为什么其他人显然通过 IE9 跨域请求成功,例如在这里:jQuery Cross-Domain Ajax JSONP Calls Failing Randomly For Unknown Reasons In Some IE Versions
-
jQuery 处理这个问题的方式,似乎在下面的代码中,但在我的特殊情况下没有调用它,不知道为什么?
// 绑定脚本标签 hack 传输 jQuery.ajaxTransport( "脚本", 函数 {
// This transport only deals with cross domain requests if ( s.crossDomain ) { 2010 年的情况类似:Jquery $.ajax fails in IE on cross domain calls 不过,这个问题应该已经被后来的 jQuery 版本解决了。
【问题讨论】:
标签: javascript jquery internet-explorer-9 cross-domain