Javascript XDomainRequest 问题
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4739384/
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
XDomainRequest problem
提问by GiaNU
I'm trying to make a asynchronous call to a service that returns json using XDomainRequest (IE8). The problem is that i always get an error (the onerror event is fired, and the responseText is always null), i'm using fiddler to check the response of the service and i seems right (I can see the json object returnig), this only happen in IE8 when using XDomainRequest, the same functionality implemented in JQuery works fine.
我正在尝试对使用 XDomainRequest (IE8) 返回 json 的服务进行异步调用。问题是我总是得到一个错误(onerror 事件被触发,并且 responseText 始终为空),我正在使用 fiddler 检查服务的响应,我看起来是对的(我可以看到 json 对象返回),这仅在 IE8 中使用 XDomainRequest 时发生,在 JQuery 中实现的相同功能工作正常。
Any clue would be appreciated. Thanks!
任何线索将不胜感激。谢谢!
P.S.: This is my javascript code:
PS:这是我的javascript代码:
.....
if (jQuery.browser.msie && window.XDomainRequest) {
//Use Microsoft XDR
var xdr = new XDomainRequest();
xdr.open("post", url);
xdr.onload = function () {
alert("Loading");
alert(xdr.responseText);
};
xdr.onsuccess = function() {
alert("Success!");
alert(xdr.responseText);
};
xdr.onerror = function() {
alert("Error!");
alert(xdr.responseText);
};
xdr.onprogress = function() {
alert("Progress");
alert(xdr.responseText);
};
xdr.timeout = 1000;
xdr.send("data: " + escape(data));
var response = xdr.responseText;
} else .....
采纳答案by Dr.Molle
Are you sure that the service is sending a Access-Control-Allow-Origin-header matching the requesting URL?
您确定该服务正在发送与请求 URL 匹配的Access-Control-Allow-Origin-header 吗?
回答by André Pedroso
Your problem may be the content-type sent, because XDomainRequest only support "text/plain".
您的问题可能是发送的内容类型,因为 XDomainRequest 仅支持“文本/纯文本”。
参考:http: //blogs.msdn.com/b/ieinternals/archive/2010/05/13/xdomainrequest-restrictions-limitations-and-workarounds.aspx
Cheers,
干杯,
Andre Pedroso
安德烈·佩德罗索
回答by Randy Skretka
A year old post,, you still there GiaNU?! You are calling xdr.onsuccess but I don't think that method exists. The xdr.onload works and seem to be equivalent to jQuery's AJAX "success" function.
一年前的帖子,你还在 GiaNU 吗?!您正在调用 xdr.onsuccess 但我认为该方法不存在。xdr.onload 有效并且似乎等同于 jQuery 的 AJAX“成功”功能。
This X-Domain stuff is quite new but there is a very nice working model available from MS now here: AJAX - Introducing Cross-domain Request (XDR)
这个 X-Domain 的东西是很新的,但是现在 MS 有一个非常好的工作模型可用:AJAX - 引入跨域请求(XDR)
The xdr.ontimeout I can't get to do a thing, but don't find a need yet :) I got things up and running first w/jQuery and now with ie9 thank's to the MS post.
xdr.ontimeout 我无法做任何事情,但还没有找到需要:) 我首先使用 jQuery 启动并运行,现在使用 ie9 感谢 MS 帖子。
The XDR has some trouble with the timing for my current application and is just used a timeout to handle it:
XDR 对我当前应用程序的计时有一些问题,只是使用超时来处理它:
xdr.onload = setTimeout( function(){ doIt( xdr.responseText ), 2000});
回答by Ciaran Bruen
Another gotcha is if you're running the service via Cassini then the "Access-Control-Allow-Origin" header won't be returned as Cassini doesn't recognise this. We had a scenario where our service calls were working on a test server but not working locally. Turns out the service was hosted in Cassini on our local dev machine but hosted on IIS on the test server.
另一个问题是,如果您通过 Cassini 运行该服务,则不会返回“Access-Control-Allow-Origin”标头,因为 Cassini 无法识别这一点。我们有一个场景,我们的服务调用在测试服务器上工作但不在本地工作。原来该服务托管在我们本地开发机器上的 Cassini 中,但托管在测试服务器上的 IIS 上。
Also here's the web.config setting for anyone that needs it (note: this allows access from any domain - "*"):
还有这里是任何需要它的人的 web.config 设置(注意:这允许从任何域访问 - “*”):
<system.webServer>
<httpProtocol>
<customHeaders>
<add name="Access-Control-Allow-Origin" value="*" />
</customHeaders>
</httpProtocol>
</system.webServer>