user187809
user187809

Reputation:

How do I get the user info of a twitter user using javascript?

Using jquery's $.ajax function, I'm not able to parse the results. For example, I used it like this

$.ajax({

    url : "http://api.twitter.com/1/users/show.json?screen_name=techcrunch",
    dataType : "json",
    success : function(data)
    {
        // parse the JSON here
    },
    error : function()
    {
        alert("Failure!");
    },

});

This doesn't work. Do I need a callback function?

Upvotes: 0

Views: 2931

Answers (2)

Jonathon Bolster
Jonathon Bolster

Reputation: 15961

As metnioned, this is due to the Same Origin Policy. To get around this, you should set your datatype to jsonp.

$.ajax({

    url : "http://api.twitter.com/1/users/show.json?screen_name=techcrunch",
    dataType : "jsonp",
    success : function(data)
    {
        console.log(data);
    },
    error : function()
    {
        alert("Failure!");
    },

});

Example: http://jsfiddle.net/jonathon/bpnbj/

Upvotes: 1

jny
jny

Reputation: 8057

You can't make an ajax call to an external url due to Same Origin Policy. You can see more info in Call external url through $.ajax in wordpres theme thread.

Upvotes: 0

Related Questions