Durathor
Durathor

Reputation: 535

404 retrieving Twitter followers using JQuery getJSON

I'm just starting to use the Twitter API to retrieve data using jQuery. I've used the API ok to retrieve information about a single user e.g. https://twitter.com/users/show/codinghorror.json

When I try to retrieve all the users that a given user is following, I'm using the same retrieval pattern but am getting a 404 error (it looks like my callback isn't receiving the json object properly, but appending it to the URL somehow)

I'm using the following code:

getTwitterUserFriends: function() {
            var user = 'codinghorror';
            var url = 'http://api.twitter.com/1/friends/ids.json?screen_name='+user+'?callback=?';
            $.getJSON(url, function(data) {
                alert('call succeeded' + data.ids);
            });
        },  

In chrome, the console shows the following error:

GET https://api.twitter.com/1/friends/ids.json?screen_name=codinghorror?callback=jQuery15201747908447869122_1324917568956&_=1324917580929 404 (Not Found)

However if I browse to the URL directly https://api.twitter.com/1/friends/ids.json?screen_name=codinghorror then I can see the results object being returned.

I assume I'm doing something simple wrong with my callback, but can't see what it is, as the approach I've used above has worked for other API calls, so any help would be much appreciated!

Upvotes: 2

Views: 699

Answers (1)

Pointy
Pointy

Reputation: 413717

Your URL syntax is incorrect. The "callback" parameter should be separated by "&", not "?".

var url = 'http://api.twitter.com/1/friends/ids.json?&screen_name='+user+'&callback=?';

You should probably URL-encode the username too:

var url = 'http://api.twitter.com/1/friends/ids.json?&screen_name=' +
  escapeURIComponent(user) + 
  '&callback=?';

Also I'm not sure why you've got a "&" before the "screen_name" parameter.

Upvotes: 1

Related Questions