views:

90

answers:

6

I'm trying to load via Ajax the last post of a twitter account. I have the url, which is :

http://twitter.com/statuses/user_timeline/myuser.json?count=1

This works :

$.get("test.php", function(data){
  alert("Data Loaded: " + data);
});

This doesn't even make a request if I monitor the console in Firebug :

$.get("http://twitter.com/statuses/user_timeline/myuser.json?count=1", function(data){
  alert("Data Loaded: " + data);
});

And I'm having the same kind of problem with .load, .post, .ajax ...

How could I do this ?


Edit :

I tried using getJSON :

$.getJSON("http://twitter.com/statuses/user_timeline/user.json?count=1",
                function(data){
                    alert("here");
                });

... still nothing, I don't even get the alert.

+2  A: 

It is possible with a JQuery plugin and YQL:

http://james.padolsey.com/javascript/cross-domain-requests-with-jquery/

Luke
if this works, that's great :)
marcgg
Too good to be true, it doesn't work. When I try to copy and paste an example, I get data.results[0] is undefined[Break on this error] responseText: data.results[0]\n
marcgg
+1  A: 

its security problem call to another url in ajax

u need to use jsonp:

jsonp

( its using jsonp with jquery )

another link of example

http://bloggingabout.net/blogs/adelkhalil/archive/2009/08/14/cross-domain-jsonp-with-jquery-call-step-by-step-guide.aspx

Haim Evgi
+3  A: 

JSONP is required for requests on other servers. Try adding &callback=? to the end of your URL.

See the manual: http://docs.jquery.com/Ajax/jQuery.getJSON You should probably be using this method anyway if you know you're going to get JSON back.

sholsinger
Peter J
marcgg
It works great ! Hurray !
marcgg
A: 

Alternatively this HowTo article may be useful.

Bryan McLemore
A: 

The correct syntax for jQuery get call is like this:

$.get("http://twitter.com/statuses/user_timeline/myuser.json", {count: "1"},           
 function(data) {
  alert("Data Loaded: " + data);
});

You have to pass the queristring parameters as a params name/value collection (the second parameter). But still you'll have the problems mentioned by the other answers.

Branislav Abadjimarinov
A: 

Depending on your needs, you can also use a Flash client-side proxy to pass your requests through. The Flash sandbox is less restrictive for cross-domain requests.

For example, see http://flxhr.flensed.com/.

atxryan
Thanks for the answer, but this sounds like overkill to me. I just want to display the last tweet of an account
marcgg