John
John

Reputation: 1

javascript XMLHttpRequest requestXML is null

I'm trying to grab an xml document from a url and then parse it. I am able to open it fine on a browser, but it doesnt seem to work through my javascript. Can anyone help me?

function downloadUrl(url, callback) {       
     var request = window.ActiveXObject ?
         new ActiveXObject('Microsoft.XMLHTTP') :
         new XMLHttpRequest;

     request.onreadystatechange = function() {
       if (request.readyState == 4) {
         request.onreadystatechange = function(){};
         callback(request, request.status);
       }
     };

    request.open('GET', "url", true);       
    request.send(null);
}   
downloadUrl("http://jojo.theone.net/survey.xml", function(data) {   
    alert("Inside downloadURL"); // shows up            
    var xml = request.responseXML;
    alert(xml);  // Doesn't even show up.
        alert(request.responseText); // Doesnt show up.


});

Upvotes: 0

Views: 898

Answers (2)

Jacob Mattison
Jacob Mattison

Reputation: 51052

You are using data as the parameter name in your callback method, but calling the callback method as callback(request, request.status). The result is that the request object is now in the var called "data", and the request.status is not referenced at all.

Try

downloadUrl("http://jojo.theone.net/survey.xml", function(request, status) {   
    alert("Inside downloadURL");
    var xml = request.responseXML;
    alert(xml); 
    alert(request.responseText);
});

Upvotes: 2

mbq
mbq

Reputation: 18628

Try to use data value not the request object. Also it is better to use some framework like Mootools or jQuery to perform AJAX requests -- you'll get a more compatible and predictable interface.
Also note that request will fail if the url you're requesting has different server, port and protocol than the script that is making request.

Upvotes: 0

Related Questions