0

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&lines=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

2 Answers 2

2

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

Sign up to request clarification or add additional context in comments.

6 Comments

ok, so will it work if I use json to fetch the data? Sorry my ignorance, I'm pretty new to this :)
@Mauro74 - nope, it has to be JSONP, which basically creates a <script> tag, looking at the documentation and testing myself...it doesn't seem there's any hint of support for this: tubeupdates.com/documentation The author of that site will have to add it, or you'll have to proxy the request through your own site.
@Nick ok I'll try to contact the author of the api to see if he could add JSONP. Anyway I have no idea on how to proxy the request, any good tutorial/advice on this? Thanks :)
@Mauro74 - depends which platform you're on, what do you have server-side? PHP, ASP.Net, ruby, other?
I'm using PHP as server-side language
|
0

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/>);

Comments

Your Answer

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.