How to parse XML from another server using JavaScript or jQuery?

Go To StackoverFlow.com

2

I am new to Javascript. I need to parse XML using javascript or jQuery which is another server. I tried by using the below code. But when I executed it, it was not going to success method.

I was able to parse the XML which is in the same folder. Is it possible in javascript to access content from another server. I read the Same Origin Policy.

I was able to get the success message, but cant get the xml data

$.ajax({

type: 'GET',
url: 'http://10.47.5.69/myxml.xml',  
dataType: "xml",

success: function(data){
    alert('success');
    $(data).find("Node").each(function() {

         alert($(this).find("element").text());
        });
    },
    error: err  
});

function err(xhr, reason, ex)
{
    alert('xhr.status: ' + xhr.status);
    alert('ex "'+ex);
}
2012-04-04 06:58
by Abi


1

You cannot load something from another server due to cross-domain security checks.

However for javascript, there is a workaround: the JSONP technique: http://en.wikipedia.org/wiki/JSONP

It is used for JSON data but it can just as well be used for any string data. But it will only work if you have some degree of control (i.e. can install a script) on that server.

Another alternative is to proxy that URL on your own server. That might be easier.

2012-04-04 08:22
by Wouter Lievens
How to proxy URL to my serve - Abi 2012-04-04 08:35
Create a dynamic page (PHP, java, whatever your backend is) on your server that fetches the XML file from the other server over HTTP, and then sends that data to your client. Check out this question on SO:

http://stackoverflow.com/questions/2102981/building-a-simple-php-url-prox - Wouter Lievens 2012-04-04 14:15

Ads