getJSON 和callback= 的确切字符串?

Dan*_*ele 1 jquery jsonp facebook

我试图让 getJSON 使用 JSONP 对象,但我不知道如何构建 url:

例如:http://graph.facebook.com/ ?ids=http://legrandj.eu/article/blouse_ghost_dream

我应该在哪里以及如何添加“callback=?” 范围?

谢谢

d.

Rob*_*b W 7

附加&callback=?到 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) {
        // ...
    }
);
Run Code Online (Sandbox Code Playgroud)

根据这段代码,jQuery 创建并插入一个<script src="http://graph.facebook.com/?ids=http://legrandj.eu/article/blouse_ghost_dream&callback=jQuery171022388557461090386_1332329918803&_=133232991983">. 网址说明:

FB API 返回以下格式的响应 ( JSONP ):

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

由于它包含在<script>标签中,因此会调用一个函数jQuery171022388557461090386_1332329918803,将解析后的 JSON 作为参数传递。然后将解析后的 JSON 传递给您在 中定义的函数jQuery.getJSON