Retrieving Remote xml data without server side script

classic Classic list List threaded Threaded
11 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Retrieving Remote xml data without server side script

salt
This post has NOT been accepted by the mailing list yet.
Hi All,

I am trying to read the remote xml but getting error. Couldnt proceed further. Please help

<script type="text/javascript"> $('document').ready(function(){ $.getJSON("http://www.w3schools.com/xml/note.xml?callback=?", function(result){ //response data are now in the result variable alert(result); }); }); </script>

Please correct me what i am doing wrong.

Thanks
salt
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Retrieving Remote xml data without server side script

Hugo <Nabble>
Administrator
This post has NOT been accepted by the mailing list yet.
Try $.get() like this:
var url = '...your URL here...';
$.get(url, function(data) {
    ...result is available here...
});
Your response should probably set the content type on the server side. In java it should be like this:
...
response.setHeader("Content-Type","text/xml");
...
You may consider using "text/plain" in your case.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Retrieving Remote xml data without server side script

salt
This post has NOT been accepted by the mailing list yet.
Hi Hugo,

Thankyou for replying. Could you please help me how to parse the data.

The xml is like this.

<note><to>Tove</to><from>Jani</from><heading>Reminder</heading><body>Don't forget me this weekend!</body></note>

How to get the heading value.?

var url = 'http://www.w3schools.com/xml/note.xml';
$.get(url, function(data) {
   alert(data);
});


Thanks
Salt
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Retrieving Remote xml data without server side script

Hugo <Nabble>
Administrator
This post has NOT been accepted by the mailing list yet.
You can use regular expressions to get that value:
var xml = "<note><to>Tove</to><from>Jani</from><heading>Reminder</heading><body>Don't forget me this weekend!</body></note>";
var heading = xml.match(/<heading>(.+)<\/heading>/)[1];
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Retrieving Remote xml data without server side script

salt
This post has NOT been accepted by the mailing list yet.
Hi Hugo,

I tried the below and get nothing.. Please help.

var url = 'http://www.w3schools.com/xml/note.xml';
$.get(url, function(xml) {
   
var heading = $(xml).match(/<heading>(.+)<\/heading>/)[1];
        alert(heading);
});

Thanks
Salt
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Retrieving Remote xml data without server side script

Hugo <Nabble>
Administrator
This post has NOT been accepted by the mailing list yet.
It should be xml.match() instead of $(xml).match().
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Retrieving Remote xml data without server side script

salt
This post has NOT been accepted by the mailing list yet.
tried both but didnt get me the results.

Code :

var url = 'http://www.w3schools.com/xml/note.xml';
$.get(url, function(xml) {
alert(xml);
var heading = xml.match(/<heading>(.+)<\/heading>/)[1];
        alert(heading);
});


for first alert, I get alerted [object Object] and the second didn't get popped.

Thanks
Salt
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Retrieving Remote xml data without server side script

Hugo <Nabble>
Administrator
This post has NOT been accepted by the mailing list yet.
The problem is that you are using ajax to retrieve information from another site (different domain name). Ajax doesn't work with third party domains. You should call a URL that is in the same domain of your app.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Retrieving Remote xml data without server side script

salt
This post has NOT been accepted by the mailing list yet.
Yes, I do understand that. I was able to get the xml data using YQL. But is there any other way we can get the data.

Thanks
Salt
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Retrieving Remote xml data without server side script

Hugo <Nabble>
Administrator
This post has NOT been accepted by the mailing list yet.
salt wrote
But is there any other way we can get the data.
This is not easy with javascript (probably not possible at all). One option is to get the XML on the server side and send it to your javascript code on the client side.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Retrieving Remote xml data without server side script

salt
This post has NOT been accepted by the mailing list yet.
Sure. Thank you for all your help.

Thanks
Salt
Loading...