Javascript XMLHTTPRequest.status 在 FireFox 3.5 中返回 0 并且 responseText 为空
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1980880/
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
XMLHTTPRequest.status returns 0 and responseText is blank in FireFox 3.5
提问by somen
I am trying to hit a third party URL to get the XML response and to show the reposne into my webpage. I get a proper response with status as 200 and readystate as 4 in IE and Safari browsers. But In FF3.5 and Crome i get XMLHTTPRequest status as 0 and reponseText comes as a blank string. I tried many options writing the normal XMLHTTPRequest Ajax code as well as using Prototype 1.5 version js file for this ajax request, but still the status and reponseText in FF 3.5 remains the same as 0 and blank string.
我正在尝试访问第三方 URL 以获取 XML 响应并将 reposne 显示到我的网页中。我在 IE 和 Safari 浏览器中得到正确的响应,状态为 200,readystate 为 4。但是在 FF3.5 和 Crome 中,我将 XMLHTTPRequest 状态设置为 0,reponseText 为空字符串。我尝试了许多选项来编写普通的 XMLHTTPRequest Ajax 代码以及使用 Prototype 1.5 版本的 js 文件来处理这个 ajax 请求,但 FF 3.5 中的状态和响应文本仍然与 0 和空白字符串相同。
Any help how to resolve this issue or what exactly is causing this issue would be greatly appreciated. I had also tried to execute my code locally as well as deploying to webserver still the repsonse in FF is same.
任何有关如何解决此问题或究竟是什么导致此问题的帮助将不胜感激。我还尝试在本地执行我的代码以及部署到网络服务器,FF 中的响应仍然相同。
Below is my code snippet
下面是我的代码片段
<script type="text/javascript" src="prototype_ajax.js"></script>
<script type="text/javascript" language="javascript">
new Ajax.Request("I place my URL Here", {
method: 'get',
onSuccess : function(transport){
var resultDoc = transport.responseText;
var rootObj = loadXML(resultDoc);
},
onFailure : function(transport){
alert(' On Failure '+transport)
}
});
function loadXML(xmlFile) {
var xmlDocElement =null;
var xmlDoc = null;
if (window.ActiveXObject) {
try {
// code for IE
xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
xmlDoc.async=false;
xmlDoc.loadXML(xmlFile);
} catch (e) {
alert("inside catch::"+e.message);
}
} else {
// code for Mozilla, Firefox, Opera, etc.
parser=new DOMParser();
xmlDoc=parser.parseFromString(xmlFile,"text/xml");
//xmlDocElement=xmlDoc.documentElement;
}
//alert('loadXML value '+xmlDoc)
return xmlDoc;
}
</script>
回答by Daniel Vassallo
It looks like you have bumped into the same origin policy. You have to use a relative path, otherwise most browsers will simply return an empty responseText.
看起来您遇到了同源策略。您必须使用相对路径,否则大多数浏览器只会返回一个空的responseText.
The following Stack Overflow post is probably also related to your problem:
以下 Stack Overflow 帖子可能也与您的问题有关:
As one possible workaround, you could set up a very simple reverse proxy(with mod_proxyif you are using Apache). This would allow you to use relative paths in your AJAX request, while the HTTP server would be acting as a proxy to any "remote" location.
作为一种可能的解决方法,您可以设置一个非常简单的反向代理(如果您使用的是 Apache,则使用mod_proxy)。这将允许您在 AJAX 请求中使用相对路径,而 HTTP 服务器将充当任何“远程”位置的代理。
The fundamental configuration directive to set up a reverse proxy in mod_proxy is the ProxyPass. You would typically use it as follows:
在 mod_proxy 中设置反向代理的基本配置指令是 ProxyPass。您通常会按如下方式使用它:
ProxyPass /web-services/ http://third-party.com/web-services/
In this case, the browser would be requesting /web-services/service.xmlbut the server would serve this by acting as a proxy to http://third-party.com/web-services/service.xml.
在这种情况下,浏览器将/web-services/service.xml发出请求,但服务器将通过充当 的代理来提供服务http://third-party.com/web-services/service.xml。

