views:

34

answers:

2

Hi there,

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.


});
A: 

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.

mbq
The url does have a different port than the script. Is there any way around this?
John
No -- this would be considered as a serious security hole, so no browser will do this. You must make a router on the server side and settle some url that will be rewritten to a different port by it.
mbq
+2  A: 

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);
});
JacobM