...

View Full Version : Parsing XML data with JS



dmac2010
01-26-2011, 01:37 PM
Hi folks..

The web app i'm developing is getting data from an XML file (JQuery) that im generating on the server side, on the fly. I want to get this XML file to parse on my app with JS. Now this is working fine when i point the location of the XML file in the SAME server, "local" to the app.

But when i try to change the XML file location to a file sitting on an external server, it doesn't seem to recognise its server location. This is prob just a JS issue that i'm not doing correctly, but can someone maybe point out anything please.

Full XML function is here: http://pastebin.com/6F3uZrBD

I'm trying:

Local side (words fine):

$.ajax({
type: "GET",
url: "channel-data.xml",
dataType: "xml",
success: function(xml) { ...

and also external server side (doesnt work):

$.ajax({
type: "GET",
url: "http://www.ExternalServerName.com/channel-data.xml",
dataType: "xml",
success: function(xml) { ...


Many thanks,
Darren.

venegal
01-26-2011, 01:57 PM
You can't do cross domain ajax requests. What you can do, though, is set up a script on your server that fetches the external xml, and get it from there.

rnd me
01-26-2011, 10:04 PM
if you add access-control headers on the remote server's xml output, then you can do cross-site ajax in all browsers newer than IE7.

dmac2010
01-26-2011, 11:11 PM
Hi all, thanks for the replies.
Rnd, can you give me an example of this?
Have you tried this already?

Regards,
Darren.

rnd me
01-27-2011, 12:13 AM
Hi all, thanks for the replies.
Rnd, can you give me an example of this?
Have you tried this already?

Regards,
Darren.

i use it all the time.

here's a good public example of using a small ajax function to grab the contents of a site i built:



function IOx(url, callBack) {
var x = new XMLHttpRequest;
if (x.withCredentials != null) {
} else if (window.XDomainRequest) {
x = new XDomainRequest;
} else {
return;
}
x.open( "get", url, true);
x.onload = callBack ? function () {callBack(x.responseText, x);} : String;
x.send(null);
return x;
}



IOx("http://www.it.illinois.edu/", function(text,ob){
alert(text);
});


(tested in IE8, FF3.6, Ch8)


if you want XML, obviously the server needs to pass the right mime-type; i couldn't find a working xml example to show, but getting there is the same for .responseText and .responseXML...



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum