XMLHTTPRequest.status 在 FireFox 3.5 中返回 0 并且 responseText 为空白
我正在尝试访问第三方 URL 以获取 XML 响应并将响应显示到我的网页中.在 IE 和 Safari 浏览器中,我得到了状态为 200 和 readystate 为 4 的正确响应.但在 FF3.5 和 Crome 中,我将 XMLHTTPRequest 状态设为 0,而 reponseText 以空白字符串的形式出现.我尝试了许多选项来编写普通的 XMLHTTPRequest Ajax 代码以及为此 ajax 请求使用 Prototype 1.5 版本的 js 文件,但 FF 3.5 中的状态和响应文本仍然与 0 和空白字符串相同.
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.
任何帮助如何解决此问题或导致此问题的确切原因将不胜感激.我还尝试在本地执行我的代码以及部署到网络服务器,但 FF 中的响应是相同的.
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.
下面是我的代码片段
<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>
推荐答案
你好像碰到了 同源政策.您必须使用相对路径,否则大多数浏览器只会返回一个空的 responseText
.
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
.
以下 Stack Overflow 帖子可能也与您的问题有关:
The following Stack Overflow post is probably also related to your problem:
- XMLHttpRequest 中的空响应文本.
作为一种可能的解决方法,您可以设置一个非常简单的反向代理(使用 mod_proxy 如果您使用的是 Apache).这将允许您在 AJAX 请求中使用相对路径,而 HTTP 服务器将充当任何远程"位置的代理.
As one possible workaround, you could set up a very simple reverse proxy (with mod_proxy if 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.
在 mod_proxy 中设置反向代理的基本配置指令是 ProxyPass.您通常会按如下方式使用它:
The fundamental configuration directive to set up a reverse proxy in mod_proxy is the ProxyPass. You would typically use it as follows:
ProxyPass /web-services/ http://third-party.com/web-services/
在这种情况下,浏览器将请求 /web-services/service.xml
,但服务器将通过充当 http://third-party 的代理来提供此服务.com/web-services/service.xml
.
In this case, the browser would be requesting /web-services/service.xml
but the server would serve this by acting as a proxy to http://third-party.com/web-services/service.xml
.
相关文章