views:

1852

answers:

1

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>
+5  A: 

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.

Daniel Vassallo