問題描述
我正在嘗試訪問第三方 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
.
這篇關于XMLHTTPRequest.status 在 FireFox 3.5 中返回 0 并且 responseText 為空白的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!