jQuery IE8/9 中带有 jQuery 和 XDomainRequest 的 CORS
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11487216/
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
CORS with jQuery and XDomainRequest in IE8/9
提问by Jeroen
UPDATE:I highly recommend not investing any time in XDomainRequest, because it is a terribly poor implementation with many limitations. It basically only really works for GET requests to non-ssl servers, so you might as well use jsonp or whatever.
更新:我强烈建议不要在 XDomainRequest 上投入任何时间,因为它是一个非常糟糕的实现,有很多限制。它基本上只适用于对非 ssl 服务器的 GET 请求,所以你不妨使用 jsonp 或其他什么。
I am using CORS to call a cross domain API, however Internet Explorer is giving issues. CORS should be possible in IE8 and IE9 through the XDomainRequest
object, however I can't get things to work..
我正在使用 CORS 来调用跨域 API,但是 Internet Explorer 出现了问题。通过XDomainRequest
对象在 IE8 和 IE9 中应该可以使用 CORS ,但是我无法让事情正常工作..
JQuery refusesto provide native support for XDomainRequest, however several jQuery plugins are suggested to add this support. This topicsuggest two such plugins: jQuery.XDomainRequest.jsand xdr.js, which has been reported to work. Afaik, the plugins should automatically override behavior of jQuery.ajax
. I found another plugin here.
JQuery拒绝为 XDomainRequest 提供本机支持,但建议使用多个 jQuery 插件来添加此支持。本主题建议使用两个这样的插件:jQuery.XDomainRequest.js和xdr.js,据报道它们可以工作。Afaik,插件应该自动覆盖jQuery.ajax
. 我在这里找到了另一个插件。
I put a little demo pages with the respective plugins jQuery.XDomainRequestand xdrand jquery.ie.corsthat perform ajax requests to a CORS enabled server. The pages are working in Chrome and Firefox, however IE8/9 instantly throw a permission denied error (even before making the request). This MSDN postsuggest adding another handler xhr.onprogress = function() {};
but I tried this and it isn't working either.
我放了一些演示页面,其中包含对启用 CORS 的服务器执行 ajax 请求的相应插件jQuery.XDomainRequest和xdr以及jquery.ie.cors。这些页面在 Chrome 和 Firefox 中工作,但是 IE8/9 立即抛出一个权限被拒绝错误(甚至在发出请求之前)。这篇MSDN 帖子建议添加另一个处理程序,xhr.onprogress = function() {};
但我试过了,它也不起作用。
Any clues what I am doing wrong? I have also tested with IE8 now using MS virtual server, but it has exactly the same problem.
任何线索我做错了什么?我现在也使用 MS 虚拟服务器对 IE8 进行了测试,但它有完全相同的问题。
Edit: OK so I figured out that part of the problem was that I was using POST over HTTPS. Apparently XDomainRequest does not allow CORS over HTTPS. I can switch to HTTP but I really need POST.
编辑:好的,所以我发现问题的一部分是我通过 HTTPS 使用 POST。显然 XDomainRequest 不允许通过 HTTPS 的 CORS。我可以切换到 HTTP,但我真的需要 POST。
Edit2: See this issue on githubfor the end of this story. It turns out that when using HTTP POST, the xDomainRequest can only encode the request body (arguments) as text/plain
. This pretty much makes it worthless, because everyone uses application/x-www-form-urlencoded
or multipart/form-data
.
Edit2:在 github 上查看这个问题以了解这个故事的结尾。事实证明,在使用 HTTP POST 时,xDomainRequest 只能将请求正文(参数)编码为text/plain
. 这几乎使它变得毫无价值,因为每个人都使用application/x-www-form-urlencoded
or multipart/form-data
。
采纳答案by Adam Alexander
POST method is supported, and to make a cross-domain https:// request your calling page would also need to be loaded over https. This is the best article I have found which explains these and other limitations of XDomainRequest in detail:
支持 POST 方法,要进行跨域 https:// 请求,您的调用页面也需要通过 https 加载。这是我找到的最好的文章,它详细解释了 XDomainRequest 的这些和其他限制:
回答by jgauffin
I've written a proxy which will gracefully downgrade to proxying if IE9 or less is used. You do not have to change your code at all if you are using ASP.NET.
我编写了一个代理,如果使用 IE9 或更低版本,它将优雅地降级为代理。如果您使用的是 ASP.NET,则根本不必更改代码。
The solution is in two parts. The first one is a jquery script which hooks into the jQuery ajax processing. It will automatically call the webserver if an crossDomain request is made and the browser is IE:
解决方案分为两部分。第一个是一个 jquery 脚本,它与 jQuery ajax 处理挂钩。如果发出跨域请求并且浏览器是 IE,它将自动调用 web 服务器:
$.ajaxPrefilter(function (options, originalOptions, jqXhr) {
if (!window.CorsProxyUrl) {
window.CorsProxyUrl = '/corsproxy/';
}
// only proxy those requests
// that are marked as crossDomain requests.
if (!options.crossDomain) {
return;
}
if (getIeVersion() && getIeVersion() < 10) {
var url = options.url;
options.beforeSend = function (request) {
request.setRequestHeader("X-CorsProxy-Url", url);
};
options.url = window.CorsProxyUrl;
options.crossDomain = false;
}
});
In your web server you have to receive the request, get the value from the X-CorsProxy-Url
http header and do a HTTP request and finally return the result.
在您的 Web 服务器中,您必须接收请求,从X-CorsProxy-Url
http 标头中获取值并执行 HTTP 请求,最后返回结果。
My blog post: http://blog.gauffin.org/2014/04/how-to-use-cors-requests-in-internet-explorer-9-and-below/
我的博文:http: //blog.gauffin.org/2014/04/how-to-use-cors-requests-in-internet-explorer-9-and-below/
回答by Gordon Freeman
For CORSrequests in IE8/9 you can use jQuery plugin jquery-transport-xdr
对于IE8/9 中的CORS请求,您可以使用 jQuery 插件jquery-transport-xdr