exact string to getJSON and callback=?

烂漫一生 提交于 2019-12-25 08:47:18

问题


I'm trying to make getJSON to use JSONP object, but I can't figure out how to build the url:

for instance: http://graph.facebook.com/?ids=http://legrandj.eu/article/blouse_ghost_dream

where and how should I add "callback=?" parameter?

thank you

d.


回答1:


Append &callback=? to the URL.

$.getJSON('http://graph.facebook.com/?ids=http://legrandj.eu/article/blouse_ghost_dream&callback=?', function(data) {
    // ..
});
// Or (more clean): 
$.getJSON('http://graph.facebook.com/?callback=?',
    {
        ids: 'http://legrandj.eu/article/blouse_ghost_dream'
    },
    function(data) {
        // ...
    }
);

Given this code, jQuery creates and inserts a <script src="http://graph.facebook.com/?ids=http://legrandj.eu/article/blouse_ghost_dream&callback=jQuery171022388557461090386_1332329918803&_=133232991983">. Explanation of the URL:

  • Base URL: http://graph.facebook.com/?ids=http://legrandj.eu/article/blouse_ghost_dream
  • Callback handler: &callback=jQuery171022388557461090386_1332329918803
    jQuery replaces ? in callback=? with an unique temporary identifier.
  • Cache-breaking: &_=133232991983

The FB API returns a response in the following format (JSONP):

/**/ jQuery171022388557461090386_1332329918803({
   "http://legrandj.eu/article/blouse_ghost_dream": {
      "id": "http://legrandj.eu/article/blouse_ghost_dream",
      "shares": 3
   }
});

Since this is included by the <script> tag, a function jQuery171022388557461090386_1332329918803 is called, passing the parsed JSON as an argument. The parsed JSON is then passed to the function which you defined in jQuery.getJSON.



来源:https://stackoverflow.com/questions/9803691/exact-string-to-getjson-and-callback

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!