0

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.
});
asked Sep 3, 2010 at 17:03
1
  • 1
    Is the script running on jojo.theone.net? Commented Sep 3, 2010 at 17:09

2 Answers 2

2

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);
});
answered Sep 3, 2010 at 17:11
Sign up to request clarification or add additional context in comments.

Comments

0

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.

answered Sep 3, 2010 at 17:09

2 Comments

The url does have a different port than the script. Is there any way around this?
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.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.