CORS与jQuery和XDomainRequest在IE8 / 9 [英] CORS with jQuery and XDomainRequest in IE8/9
问题描述
更新:我强烈建议不要在XDomainRequest中投入任何时间,因为它是一个非常差的实施,有很多限制。
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.
JQuery 拒绝为XDomainRequest提供原生支持,但建议添加此支持的多个jQuery插件。此主题建议使用两个这样的插件: jQuery.XDomainRequest.js 和 xdr.js ,它已被报告工作。 Afaik,插件应该自动覆盖 jQuery.ajax
的行为。我找到了另一个插件这里。
JQuery refuses to provide native support for XDomainRequest, however several jQuery plugins are suggested to add this support. This topic suggest two such plugins: jQuery.XDomainRequest.js and 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 和 xdr 和 jquery.ie.cors 向CORS启用的服务器执行ajax请求。这些页面在Chrome和Firefox中正常工作,但IE8 / 9立即抛出一个权限被拒绝错误(即使在发出请求之前)。此 MSDN帖子建议添加另一个处理程序 xhr.onprogress = function(){};
但我试过这个,它也不工作。
I put a little demo pages with the respective plugins jQuery.XDomainRequest and xdr and jquery.ie.cors that 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 post suggest adding another handler xhr.onprogress = function() {};
but I tried this and it isn't working either.
任何线索我做错了什么?我也已经测试过IE8现在使用MS虚拟服务器,但它有完全相同的问题。
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.
编辑:好吧,所以我想出了问题的一部分,通过HTTPS使用POST。显然XDomainRequest不允许通过HTTPS的CORS。我可以切换到HTTP,但我真的需要POST。
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.
Edit2:请参阅此问题在github 结束这个故事。事实证明,当使用HTTP POST时,xDomainRequest只能将请求主体(参数)编码为 text / plain
。这几乎使它没有价值,因为每个人都使用 application / x-www-form-urlencoded
或 multipart / form-data
。
See this issue on github for 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
.
推荐答案
支持POST方法,要创建跨网域https://请求,您的呼叫页面还需要通过https加载。这是我找到的最好的文章,详细解释XDomainRequest的这些和其他限制:
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:
这篇关于CORS与jQuery和XDomainRequest在IE8 / 9的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!