marcgg
marcgg

Reputation: 66436

How to do a $.get on a remote website?

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.

Upvotes: 2

Views: 7087

Answers (5)

Ryan Joy
Ryan Joy

Reputation: 3039

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/.

Upvotes: 0

Branislav Abadjimarinov
Branislav Abadjimarinov

Reputation: 5131

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.

Upvotes: 2

sholsinger
sholsinger

Reputation: 3078

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.

Upvotes: 4

Haim Evgi
Haim Evgi

Reputation: 125446

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

Upvotes: 1

Luke
Luke

Reputation: 3381

It is possible with a JQuery plugin and YQL:

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

Upvotes: 2

Related Questions