如何使用JSONP查询Facebook Graph API [英] How to query the Facebook Graph API with JSONP

查看:147
本文介绍了如何使用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.

这篇关于如何使用JSONP查询Facebook Graph API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆