-1

can any one please tell me why this jQuery function is not working. It raises no alert.

$.getJSON('http://api.twitter.com/1/statuses/user_timeline.json?',
  {
      screen_name: 'samajshekhar',
      count: '5',
  },
 function (data) {
      alert('hello world from twitter');
  });   

In fiddler i can see that expected JSON has been returned.

However when calling flicker's api using the example code at jQuery documentation it gives an alert as expected

$.getJSON('http://api.flickr.com/services/feeds/photos_public.gne?jsoncallback=?',
  {
    tags: 'cat',
    tagmode: 'any',
    format: 'json'
  },
  function(data) {
   alert('hello world from flicker');   
  });

Here is the sample code on JS Bin

I tried even with facebook's graph API still no alert

In sample code i tried getJSON calls to facebook,twitter,flicker and only the flicker's call's alert is raised.

Shekhar_Pro
  • 18,056
  • 9
  • 55
  • 79

3 Answers3

9

You need to append a callback parameter to the url so that it becomes a jsonp call.

http://api.twitter.com/1/statuses/user_timeline.json?callback=?
Marcus Frödin
  • 12,552
  • 3
  • 25
  • 16
  • It should work. This worked fine for me, for example: api.twitter.com/1/statuses/user_timeline.json?screen_name=marcusf&callback=function_i_want_to_run. Gave me correct jsonp response. – Marcus Frödin Mar 03 '11 at 17:01
  • yeah it worked..thanx.. it didn't worked with jsoncallback but worked with callback.. any explanation.. i a newbie to jQuery ^_^ – Shekhar_Pro Mar 03 '11 at 17:08
  • i got my answer from here http://stackoverflow.com/questions/2067472/please-explain-jsonp – Shekhar_Pro Mar 03 '11 at 17:22
3

The difference between these two examples is that the Flickr API call is returning a JSONP result, while the Twitter API call is returning a JSON result. The Twitter call, because the response is JSON, is violating the Same Origin Policy which prevents running scripts which originate from another domain; JSONP responses, on the other hand, are not governed by this rule.

As the other users have indicated, append '?callback=?' to the request URL and the Twitter API response will take the form of JSONP.

Nathan Taylor
  • 24,423
  • 19
  • 99
  • 156
  • can you explain why flicker works with `jsoncallback=?` while facebook and twitter work with `callback=?` in url – Shekhar_Pro Mar 03 '11 at 17:13
  • anyway i got my answer from here http://stackoverflow.com/questions/2067472/please-explain-jsonp – Shekhar_Pro Mar 03 '11 at 17:21
  • @Shekhar_Pro Nice link, I found that helpful myself. As the author indicates, whether the response is returned as JSON or JSONP depends on how the server treats your request. In the case of Flickr and Twitter, the former's API expects a parameter called 'jsoncallback', while the latter expects just 'callback' – Nathan Taylor Mar 03 '11 at 17:27
2

You do not have the JSONP parameter in the url

$.getJSON('http://api.twitter.com/1/statuses/user_timeline.json?jsoncallback=?',
epascarello
  • 204,599
  • 20
  • 195
  • 236