Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
375 views
in Technique[技术] by (71.8m points)

javascript - XMLHTTPRequest.status returns 0 and responseText is blank in FireFox 3.5

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.

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.

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>
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.

The following Stack Overflow post is probably also related to your problem:

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.

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/

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...