Facebook的图形API获得JSON使用Ajax [英] Facebook Graph API getting JSON using Ajax
问题描述
这是我第一次在jQuery中使用Facebook的。
我收到以下错误:
未捕获的SyntaxError:意外的标记:
www.facebook.com/feeds/page.php?id=20531316728&format=JSON
&功放;回调= jQuery110105899784066714346_1383828332964和放大器; _ = 1383828332965:2
code
$。阿贾克斯({
网址:http://www.facebook.com/feeds/page.php?id=20531316728&format=JSON,
数据类型:JSONP
})。完成(功能(数据){
警报(数据);
});
为什么会出现这个?
为什么不工作?
jQuery的,是一个JavaScript框架,必须应用实施细则的Ajax请求,更具体的相同的起源一>之一。简单地说,这种限制说,Ajax请求只能向同一个域中进行。
这信息也可以在jQuery的找到 $。阿贾克斯
文档:
由于浏览器的安全限制,大多数Ajax的要求,则须 以同样的原产地政策;请求不能成功地检索 从不同的域,子域,或协议数据。
解决方法
YQL:雅虎查询语言
雅虎查询语言是一个前pressive类似于SQL的语言,可以让 您查询,过滤器,并加入跨Web服务的数据。随着YQL,应用程序 与code行较少和较小的网络覆盖跑得更快。
使用YQL作为代理
YQL可以用作代理,以使一个跨域Ajax呼叫成为可能。说明可以在这里找到:的JavaScript:使用Web代理跨域XMLHtt prequest电话
code
VAR fbUrl =http://www.facebook.com/feeds/page.php?id=20531316728&format=JSON;
$阿贾克斯({
网址:http://query.yahooapis.com/v1/public/yql
数据类型:JSONP
数据: {
问:'+ fbUrl +'从JSON其中,url =选择*',
格式为:JSON
},
成功:功能(数据){
$每个(data.query.results.json.entries,功能(I,V){
$('#项目)追加(data.query.results.json.entries [I] .title伪+'< BR />');
});
}
});
<大骨节病> 的jsfiddle这里
This is my first time using Facebook in jQuery.
I'm getting the following error:
Uncaught SyntaxError: Unexpected token :
www.facebook.com/feeds/page.php?id=20531316728&format=JSON
&callback=jQuery110105899784066714346_1383828332964&_=1383828332965:2
Code
$.ajax({
url: 'http://www.facebook.com/feeds/page.php?id=20531316728&format=JSON',
dataType: 'jsonp'
}).done(function(data) {
alert(data);
});
Why am I getting this?
Why doesn't it work?
jQuery, being a Javascript framework, must apply the implementation rules for Ajax requests, more specifically the Same-origin policy one. Briefly, this restriction says that Ajax requests can only be performed towards the same domain.
This information can also be found in the jQuery $.ajax
documentation:
Due to browser security restrictions, most "Ajax" requests are subject to the same origin policy; the request can not successfully retrieve data from a different domain, subdomain, or protocol.
Workaround
YQL: Yahoo Query Language
The Yahoo Query Language is an expressive SQL-like language that lets you query, filter, and join data across Web services. With YQL, apps run faster with fewer lines of code and a smaller network footprint.
Source: http://developer.yahoo.com/yql/
Using YQL as a proxy
YQL can be used as a proxy in order to make a cross-domain Ajax call possible. Explanations can be found here: JavaScript: Use a Web Proxy for Cross-Domain XMLHttpRequest Calls
Code
var fbUrl = "http://www.facebook.com/feeds/page.php?id=20531316728&format=JSON";
$.ajax({
url: "http://query.yahooapis.com/v1/public/yql",
dataType: "jsonp",
data: {
q: 'select * from json where url="' + fbUrl + '"',
format: "json"
},
success: function (data) {
$.each(data.query.results.json.entries, function (i, v) {
$('#entries').append(data.query.results.json.entries[i].title + '<br />');
});
}
});
这篇关于Facebook的图形API获得JSON使用Ajax的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!