javascript jQuery .ajax 在 IE8/9 中跨域请求拒绝访问
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22519736/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
jQuery .ajax getting Access Denied on Cross Domain Request in IE8/9
提问by twalters
I am using jQuery's .ajax to pull in content from other pages into a modal window. The majority of these pages are on the same domain and work correctly across all browsers.
我正在使用 jQuery 的 .ajax 将其他页面的内容拉入模态窗口。这些页面中的大多数都在同一个域中,并且可以在所有浏览器中正常工作。
There is one link that goes to another domain. That link works correctly in Chrome, Firefox, and Safari but gives an Access Denied error in Internet Explorer 8 and 9.
有一个链接到另一个域。该链接在 Chrome、Firefox 和 Safari 中正常工作,但在 Internet Explorer 8 和 9 中出现拒绝访问错误。
I searched quite a bit for a solution and tried several things that have not helped so far including:
我搜索了很多解决方案并尝试了一些到目前为止没有帮助的事情,包括:
- I confirmed Access-Control-Allow-Origin was set to allow my domain. I tried setting it to "*" as well just to confirm that wasn't the issue.
- I tried using different jQuery methods to make the AJAX call. I used jQuery's load, get, and ajax methods. jQuery's ajax method gave me the Access Denied error.
- I tried using Internet Explorer's XDomainRequests. XDomainRequests told me there was an error but gave me no more information. I tried several changes to this code I found suggestions for like setting a timeout on the send function but no change.
- I set jQuery.support.cors = true; and added crossDomain: true to my .ajax call.
- I found someone who suggested that jQuery version 1.8.0 caused the issue which happens to be the version that is used on the website I am working on. The suggestion they had was to step back to version 1.7.2 which did not fix the issue. I also tried changing to version 1.11.0 but that also did not make a difference.
- I saw a suggestion that adding &callback=? to the end of the url would solve it but that didn't make a difference.
- 我确认 Access-Control-Allow-Origin 设置为允许我的域。我也尝试将其设置为“*”,以确认这不是问题。
- 我尝试使用不同的 jQuery 方法进行 AJAX 调用。我使用了 jQuery 的 load、get 和 ajax 方法。jQuery 的 ajax 方法给了我拒绝访问错误。
- 我尝试使用 Internet Explorer 的 XDomainRequests。XDomainRequests 告诉我有一个错误,但没有给我更多信息。我尝试对此代码进行多次更改,我发现了一些建议,例如在发送功能上设置超时但没有更改。
- 我设置 jQuery.support.cors = true; 并将 crossDomain: true 添加到我的 .ajax 调用中。
- 我发现有人建议 jQuery 1.8.0 版导致了这个问题,这恰好是我正在处理的网站上使用的版本。他们的建议是退回到没有解决问题的 1.7.2 版。我也尝试更改为 1.11.0 版,但这也没有任何区别。
- 我看到一个建议添加 &callback=? 到 url 的末尾会解决它,但这并没有什么区别。
I am probably forgetting something that I tried today but that should be most of it.
我可能忘记了我今天尝试过的一些事情,但这应该是其中的大部分内容。
Here is the code I have at the moment:
这是我目前拥有的代码:
jQuery.support.cors = true;
var request = $.ajax(
{
crossDomain: true,
type: "GET",
url: url,
success: function () {
console.log('success');
},
error: function (a, status, error) {
console.log('error: ' + error);
},
complete: function () {
console.log('complete');
}
});
Chrome, Firefox, and Safari log 'success' and 'complete' in the console. IE8/9 log 'error: Error: Access is denied.' and 'complete' in the console.
Chrome、Firefox 和 Safari 在控制台中记录“成功”和“完成”。IE8/9 日志“错误:错误:访问被拒绝。” 并在控制台中“完成”。
回答by ConfusedCoder
I ran into similar problem the other day. This is what I tried and it worked like a charm
前几天我遇到了类似的问题。这就是我尝试过的,它就像一个魅力
I had the attribute dataType set to jsonp (i.e. dataType: 'jsonp') this will work for GET but not for POST
我将属性 dataType 设置为 jsonp(即 dataType: 'jsonp')这适用于 GET 但不适用于 POST
If the above still doesn't work then you can reference below script from MoonScript and it hooks the support up automatically
如果以上仍然不起作用,那么您可以从 MoonScript 引用以下脚本,它会自动连接支持
https://github.com/MoonScript/jQuery-ajaxTransport-XDomainRequest
https://github.com/MoonScript/jQuery-ajaxTransport-XDomainRequest
回答by 1derboy1
I used this to make internet explorer work for me on cross domain ajax calls. I'd be happy to list my solution here as well, but I think that the link below probably explains in detail enough from a more intelligent user than me.
我用它来让 Internet Explorer 在跨域 ajax 调用中对我来说有效。我也很乐意在这里列出我的解决方案,但我认为下面的链接可能从比我更聪明的用户那里得到了足够详细的解释。