Javascript 拒绝访问 IE 上的 jQuery 脚本
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5087549/
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
Access denied to jQuery script on IE
提问by Kyle
I have an iframe using the jQuery 1.4.2 script. The same iframe is injected into both http
and https
sites. The jQuery script is included in the main HTML file as a relative path (e.g., /scripts/jquery-1.4.2.min.js).
我有一个使用 jQuery 1.4.2 脚本的 iframe。相同的 iframe 被注入到http
和https
站点。jQuery 脚本作为相对路径包含在主 HTML 文件中(例如,/scripts/jquery-1.4.2.min.js)。
When an AJAX call is made, Internet Explorer denies access. The AJAX is calling on another subdomain, but it's using the right protocol. All other browsers work but Internet Explorer gives the following error:
进行 AJAX 调用时,Internet Explorer 拒绝访问。AJAX 正在调用另一个子域,但它使用了正确的协议。所有其他浏览器都可以工作,但 Internet Explorer 出现以下错误:
SCRIPT5: Access is denied.
jquery-1.4.2.min.js, line 127 character 344
SCRIPT5:访问被拒绝。
jquery-1.4.2.min.js,第 127 行字符 344
I heard this error is from cross-domain AJAX calls. But why is IE the only one giving me crap? Is there an IE solution?
我听说这个错误来自跨域 AJAX 调用。但为什么 IE 是唯一一个给我废话的人?有 IE 解决方案吗?
Also, this is my AJAX:
另外,这是我的 AJAX:
$.ajax({
url: thisURL,
dataType: "json",
data: {cmd : 'getMessage', uurl: urlVar, t: Math.random()},
success: function(ret){
callback(ret)
}
});
采纳答案by Rafay
IE requires you to use XDomainRequest instead of XHR for cross site, you can try something like...
IE 要求您使用 XDomainRequest 而不是 XHR 跨站点,您可以尝试类似...
if ($.browser.msie && window.XDomainRequest) {
// Use Microsoft XDR
var xdr = new XDomainRequest();
xdr.open("get", url);
xdr.onload = function() {
// XDomainRequest doesn't provide responseXml, so if you need it:
var dom = new ActiveXObject("Microsoft.XMLDOM");
dom.async = false;
dom.loadXML(xdr.responseText);
};
xdr.send();
} else {
// your ajax request here
$$.ajax({
url: thisURL,
dataType: "json",
data: {cmd : 'getMessage', uurl: urlVar, t: Math.random()},
success: function(ret){
callback(ret)
}
});
}
Reference
参考
http://forum.jquery.com/topic/cross-domain-ajax-and-ie
http://forum.jquery.com/topic/cross-domain-ajax-and-ie
not sure whether it fits your scenario
不确定它是否适合您的场景
xdr = new XDomainRequest();
xdr.onload=function()
{
alert(xdr.responseText);
}
xdr.open("GET", thisUrl); //thisURl ->your cross domain request URL
//pass your data here
xdr.send([data]);
you can find some more guidance here
你可以在这里找到更多指导
回答by Kevin Leary
This solved the issue gracefully for me:
这为我优雅地解决了这个问题:
https://github.com/MoonScript/jQuery-ajaxTransport-XDomainRequest
https://github.com/MoonScript/jQuery-ajaxTransport-XDomainRequest
Just install/compile after jQuery and before your script and use the $.ajax method as you normally would, the rest is handled behind the automatically.
只需在 jQuery 之后和脚本之前安装/编译,并像往常一样使用 $.ajax 方法,其余的在后面自动处理。
回答by matthieuR42
Have you try to use the lastest of JQuery(> jquery-1.8.0)? Since the version 1.8.0, they solved some IE9's bugs. Perhaps this one too.
您是否尝试使用最新的 JQuery(> jquery-1.8.0)?从 1.8.0 版本开始,他们解决了 IE9 的一些错误。或许这个也是。
回答by iCoder
I was facing similar issue. I was using file upload control but it was hidden and I had another element trying to control the file upload and events to upload file in ajax way
我面临着类似的问题。我正在使用文件上传控件,但它被隐藏了,我有另一个元素试图控制文件上传和事件以 ajax 方式上传文件
try using the file upload control directly. this solved issue in my application.
尝试直接使用文件上传控件。这解决了我的应用程序中的问题。
回答by Emmanuel Gleizer
I get this bug (and thus google here) but the reason was very different. So if you don't have cross site and still get this access denied error: double check the valuesent
let's say that you affect one of you variable with the bad following expression:
我得到了这个错误(因此在这里谷歌)但原因非常不同。因此,如果您没有跨站点并且仍然收到此访问被拒绝错误:请仔细检查发送的值,
假设您使用以下错误表达式影响了其中一个变量:
urlVar = $("theID").val // without () this was the error!
[...]ajax call:
[...]ajax 调用:
data: {cmd : 'getMessage', uurl: urlVar, t: Math.random()},
Google/FF have no problem with this (check what is receive server side...) BUT IE refuse to send this!
谷歌/FF对此没有问题(检查接收服务器端是什么......)但IE拒绝发送!
回答by Furqan Hameedi
Check the domain you are accessing, following response headers should be there
检查您正在访问的域,以下响应标头应该在那里
"Access-Control-Allow-Methods" : "POST, GET, OPTIONS"
"Access-Control-Allow-Origin" : "http://www.mydomain.com" or "*"
the other domain should allow your script request. One more header to be added to your response is P3P header.
另一个域应该允许您的脚本请求。要添加到您的响应中的另一个标头是 P3P 标头。
"p3p" : "CP=IDC DSP COR ADM DEVi TAIi PSA PSD IVAi IVDi CONi HIS OUR IND CNT"
it should help you out.
它应该可以帮助你。
回答by James Becwar
I had a similar problem and the solution for me was to use jsonp instead of json. That way I didn't have to break out a customer version for IE.
我有一个类似的问题,我的解决方案是使用 jsonp 而不是 json。这样我就不必为 IE 开发一个客户版本。
You can only do this if the json server host supports the callback request variable or you have access to the server and can add support. Here is a page that helped me understand the process. Its .net mvc focused, but it gives a good over view of the diffrence between json and jsonp.
只有当 json 服务器主机支持回调请求变量或者您有权访问服务器并且可以添加支持时,您才能执行此操作。这是一个帮助我理解过程的页面。它的 .net mvc 专注于,但它可以很好地了解 json 和 jsonp 之间的差异。
http://blogorama.nerdworks.in/entry-EnablingJSONPcallsonASPNETMVC.aspx
http://blogorama.nerdworks.in/entry-EnablingJSONPcallsonASPNETMVC.aspx
回答by Trolloc
I changed my JQuery from version 1.10.1to 1.10.2and it seems to have solved this problem for me.
我将 JQuery 从1.10.1版更改为1.10.2版,它似乎为我解决了这个问题。
回答by AzuraStar
Simply add 'callback=?' on your ajax URL request like here: http://wsvdmeer.blogspot.com.es/2012/08/bugfix-getjson-not-working-in-ie.html
只需添加 'callback=?' 在您的 ajax URL 请求上,如下所示:http: //wsvdmeer.blogspot.com.es/2012/08/bugfix-getjson-not-working-in-ie.html
回答by Jez D
It seems that MS is finding its own way of doing things, rather than adopting industry recommendations. I found the solution here:
https://github.com/MoonScript/jQuery-ajaxTransport-XDomainRequest/blob/master/jQuery.XDomainRequest.js
似乎 MS 正在寻找自己的做事方式,而不是采用行业建议。我在这里找到了解决方案:https:
//github.com/MoonScript/jQuery-ajaxTransport-XDomainRequest/blob/master/jQuery.XDomainRequest.js