問(wèn)題描述
我正在嘗試訪問(wèn)第三方 URL 以獲取 XML 響應(yīng)并將響應(yīng)顯示到我的網(wǎng)頁(yè)中.在 IE 和 Safari 瀏覽器中,我得到了狀態(tài)為 200 和 readystate 為 4 的正確響應(yīng).但在 FF3.5 和 Crome 中,我將 XMLHTTPRequest 狀態(tài)設(shè)為 0,而 reponseText 以空白字符串的形式出現(xiàn).我嘗試了許多選項(xiàng)來(lái)編寫普通的 XMLHTTPRequest Ajax 代碼以及為此 ajax 請(qǐng)求使用 Prototype 1.5 版本的 js 文件,但 FF 3.5 中的狀態(tài)和響應(yīng)文本仍然與 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.
任何幫助如何解決此問(wèn)題或?qū)е麓藛?wèn)題的確切原因?qū)⒉粍俑屑?我還嘗試在本地執(zhí)行我的代碼以及部署到網(wǎng)絡(luò)服務(wù)器,但 FF 中的響應(yīng)是相同的.
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>
推薦答案
你好像碰到了 同源政策.您必須使用相對(duì)路徑,否則大多數(shù)瀏覽器只會(huì)返回一個(gè)空的 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 帖子可能也與您的問(wèn)題有關(guān):
The following Stack Overflow post is probably also related to your problem:
- XMLHttpRequest 中的空響應(yīng)文本.
作為一種可能的解決方法,您可以設(shè)置一個(gè)非常簡(jiǎn)單的反向代理(使用 mod_proxy 如果您使用的是 Apache).這將允許您在 AJAX 請(qǐng)求中使用相對(duì)路徑,而 HTTP 服務(wù)器將充當(dāng)任何遠(yuǎn)程"位置的代理.
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 中設(shè)置反向代理的基本配置指令是 ProxyPass.您通常會(huì)按如下方式使用它:
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/
在這種情況下,瀏覽器將請(qǐng)求 /web-services/service.xml
,但服務(wù)器將通過(guò)充當(dāng) http://third-party 的代理來(lái)提供此服務(wù).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
.
這篇關(guān)于XMLHTTPRequest.status 在 FireFox 3.5 中返回 0 并且 responseText 為空白的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!