jquery how to get data from API xml
I need to get data from an API which returns xml format. here's the api url: http://api.tubeupdates.com/?method=get.status&lines=all&format=xml
here's my jquery code:
$(document).ready(function(){
$.get('http://api.tubeupdates.com/?method=get.status&line开发者_JAVA技巧s=all&format=xml', function(d){
$(d).find('line').each(function(){
var $line = $(this).find('name').text()
var $mex = $(this).find('message').text()
$('#status').append($line+'<br>'+$mex+'<br><br>');
});
});
});
The div #status returns empty. My jquery code seems fine because with a physical xml file it works. What am I doing wrong?
Thanks in advance,
Mauro
You can't fetch XML cross-domain like this, you'll have to use JSONP if the server supports it. XML has to be fetched via an XMLHttpRequest...which is blocked by the same origin policy.
Unfortunately, it doesn't look like that site supports JSONP, though it does support regular JSON: http://api.tubeupdates.com/?method=get.status&lines=all&format=json
Remove the $
from this variable line and add:
var line = $(this).find('name').text();
var mes = $(this).find('message').text();
$('#status').append(line + '<br/>' + mes + <br/>);
精彩评论