开发者

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

开发者 https://www.devze.com 2022-12-16 11:43 出处:网络
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

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

An开发者_如何学God 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.


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.


It is possible with a JQuery plugin and YQL:

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


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.


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


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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号