views:

30

answers:

1

Hi, I need to load xml file from specified url using javascript. Here is what i am doing:

function GetXMLDoc() {
    var url = 'http://www.warm.fm/exports/NowOnAir.xml';
    var httpRequest = null;
    try {
        httpRequest = new ActiveXObject('Msxml2.XMLHTTP'); 
    }
    catch (e) {
        try {
            httpRequest = new ActiveXObject('Microsoft.XMLHTTP'); 
        }
        catch (e2) {
            try {
                httpRequest = new XMLHttpRequest(); 
            }
            catch (e3) { httpRequest = false; }
        }
    }
    if (httpRequest) {
        httpRequest.open('POST', url, false);
        httpRequest.setRequestHeader("Content-Type", "text/xml");
        httpRequest.send(null);
        alert(httpRequest.responseText);
    }
    else {
        alert(httpRequest);
    }

It works perfectly in IE but it does not in FF and Google Chrome. Firebug shows me the following error:

*uncaught exception: [Exception... "Component returned failure code: 0x80004005 (NS_ERROR_FAILURE) [nsIXMLHttpRequest.send]" nsresult: "0x80004005 (NS_ERROR_FAILURE)" location: "JS frame :: http://localhost:49697/XMLParser.js :: GetXMLDoc :: line 43" data: no]*

Is there anyone who has the answer that will help me in solving the issue?

Thanks, Mohin

+2  A: 
    httpRequest.open('POST', url, false);
    httpRequest.setRequestHeader("Content-Type", "text/xml");
    httpRequest.send(null);

This doesn't really make sense. You're doing a POST and claiming that you're sending an XML file as the request body, but then sending nothing.

I suggest you really want to do a simple GET:

    httpRequest.open('GET', url, false);
    httpRequest.send();

Naturally you will have to do this from a document on www.warm.fm to satisfy the same origin policy; localhost won't work.

And I would seriously reconsider the synchronousness of the request (open...false). This is freeze the browser whilst the file is fetched, which is pretty user-hostile. Asynchronous requests with an onreadystatechange callback are almost always preferable.

Also, the cross-browser xmlhttprequest stuff is a bit old fashioned, and tries ActiveXObject first. Native XMLHttpRequest is usually the one to go for first. Try this IE6 fallback code instead:

if (!window.XMLHttpRequest && 'ActiveXObject' in window) {
    window.XMLHttpRequest= function() {
        return new ActiveXObject('MSXML2.XMLHttp');
    }
}

then you can just do new XMLHttpRequest() on any browser.

bobince
Hi Thanks for your valuable reply. Actually I am using false here because I don't want it to be asynchronous. I have used httpRequest.open('GET', url, false);httpRequest.send(); but still same problem. I need to get my code working on FF as it is working on IE
Mohin
Where are you running the script from? As mentioned above it will definitely not work from localhost to www.warm.fm due to the Same Origin Policy. For some reason you get the NS_ERROR_FAILURE when you try to do a blocked synchronous request whereas with a asynchronous request you get the `status=0` failure instead. Either way though, it won't work.
bobince
I understand what you are saying. But you know it works it is working from localhost for IE. Do you have any idea why it is working on IE from localhost?
Mohin
If `localhost` is in a trusted zone it may get looser security settings in IE. No other browser uses ‘zones’, and it'll break as soon as you deploy it at a normal internet address.
bobince
so, you are saying that when i deploy my work on live server, it will work fine?
Mohin
Only if your live server is `www.warm.fm`.
bobince
so u r saying that it is not possible to make it work from another server?
Mohin
You can only do a request from a hostname to the same hostname. That's a fundamental property of `XMLHttpRequest`—it would be a huge security risk if you were allowed to fetch documents from other sites using the user's browser (and hence security credentials; you could send a request to their bank to view their account, for example).
bobince
There are extensions to allow cross-site requests to be done safely, such as IE8's XDomainRequest and W3's XMLHttpRequest Level 2 standard for other browsers, but support isn't good enough yet and in any case they require the target site to specifically opt-in. Sites that want to provide data to third-party pages today typically provide a script-inclusion interface, known as “JSONP”.
bobince
For sites that don't provide a third-party interface, the only possibility is to send a request to your own server, which will make the HTTP request on the client's behalf (without client credentials, so it's safe). This is effectively using your server as a proxy. If your server starts bombarding the target with requests you may end up getting blocked, so be considerate about request rate and cache data you've fetched from the target when you can.
bobince
thanks a lot for ur answer. I have already tested what u said. thanks a lot for your valuable time
Mohin