0

I am trying to parse an RSS feed using javascript, and I recently ran into a problem opening an XML file from both localhost and web server (I always access my local files using http:// keyword all the time when I work on localhost). I received XML status 0 instead of status 200 (established) in all browser. The problem only occurs when I try to open an XML file hosted on a server. Here is what I have :

if(window.ActiveXObject)//IE
    xml_req = new ActiveXObject("Microsoft.XMLHTTP");
else if(window.XMLHttpRequest)
    xml_req = new XMLHttpRequest();
    else alert('no ajax support');

xml_req = new XMLHttpRequest();
var url = "http://www.something.net/rss.xml";
xml_req.open("GET", url, true);

xml_req.onreadystatechange = function(){
    if(xml_req.readyState == 4){
        if(xml_req.status == 200){
            if(xml_req.responseText != null)
                .....//DO STUFF
            else{
                alert("Error");
                return false;
            }
        }
        else{
                  //THIS IS WHERE THE ERROR OCCURS,STATUS=0 INSTEAD OF 200
               alert("XML Status = " + xml_req.status + xml_req.statusText);}
    }
}

Everything works fine when I open an XML local file that is not hosted on the server. For instance, the code below opens the rss file within the localhost just fine :

var url = "rssLocalhost.xml";
xml_req.open("GET", url, true);

Please help! Would appreciate any suggestions from anybody out there. Thanks!

1 Answer 1

1

You can't make requests to other sites. You either need to proxy the data through your own server, or use a JSON-P data source.

3
  • It's really helpful, thanks for the answer. So if I try to consume the data from JSON-P data source, is the data going to be in raw XML or do I have to change some of the codes in my parser? (since my parser currently only parses XML files) Commented Apr 28, 2011 at 10:16
  • If you have a JSON-P datasource then it will be in JSON-P, not XML. Commented Apr 28, 2011 at 10:21
  • yeah looks like it would've been much easier had I used JSONP, but it was a good experience for me though :) Commented Apr 28, 2011 at 19:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.