如何使用JSONP查询Facebook Graph API

问题描述:

不应该使用JQuery工作遵循AJAX请求?

Shouldn't follow AJAX request with JQuery work?

$.getJSON('https://graph.facebook.com/138654562862101/feed?callback=onLoadJSONP');

我已经定义了一个名为 onLoadJSONP的回调函数

I have defined a callback function named onLoadJSONP.

但是Chrome给了我一个典型的相同原因 - 政策错误:

But Chrome gives me a typical Same-Origin-Policy error:


XMLHttpRequest无法加载
https://graph.facebook.com/138654562862101/喂?回调= onLoadJSONP
原始null不允许
Access-Control-Allow-Origin。

XMLHttpRequest cannot load https://graph.facebook.com/138654562862101/feed?callback=onLoadJSONP. Origin null is not allowed by Access-Control-Allow-Origin.

我以为JSONP工作那我做错了什么?

I thought JSONP worked around that, what am I doing wrong?

jQuery检测JSONP所需的行为 具体 callback =? ,所以你会需要 ,然后传递要处理的功能。没有任何外部变更,您可以执行以下操作:

jQuery detects JSONP desired behavior specifically with callback=?, so you'll need exactly that, then pass the function you want to handle it. With no outside changes, you could do this:

$.getJSON('https://graph.facebook.com/138654562862101/feed?callback=?', onLoadJSONP);

这可以搜索 callback =?通过直接使用你的函数作为回调来继续工作。以前,没有检测到你想要一个JSONP提取,并试图使用一个XMLHttpRequest来抓取数据...由于相同的起始策略限制而失败。

This allows the search for callback=? to still work by using your function as the callback directly. Previously, it wasn't detecthing that you wanted a JSONP fetch, and was trying to use an XMLHttpRequest to grab the data...which fails due to the same origin policy restriction.