使用新的facebook图形API,ajax调用返回null(空) [英] Using the new facebook graph api, ajax calls returns null (empty)

查看:91
本文介绍了使用新的facebook图形API,ajax调用返回null(空)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试了新的图表api为Facebook。我试图使用jquery ajax获取一些数据。
这是我的JavaScript代码的示例,非常基本...

Im trying out the new graph api for facebook. I'm trying to fetch some data using jquery ajax. This is a sample of my javascript code, very basic...

var mUrl = 'https://graph.facebook.com/19292868552';
   $.ajax({
        url: mUrl,
        dataType: 'json',
        success: function(data, status) {
          $('#test').html(data);
          alert(data);

      },
      error: function(data, e1, e2) {
        $('#hello').html(e1);  
      }
   });

该网址是不需要访问令牌的网页(使用浏览器尝试),但是success函数返回一个空对象或null。

The url is to a page that does not need access tokens (try it using a browser), but the success function returns an empty object or null.

我做错了什么?感谢所有的帮助!

What am I doing wrong? Thankful for all help!

推荐答案

我已经解决了这个问题。我做了一个快速的教程,其中涵盖了这一点,全部解释

I have covered this and asked this question before. I made a quick tutorial which covers exactly this and explains it all.

简而言之:
根据同源策略,JSON不适用于跨域使用。但是,使用JSONP,我们可以在jQuery中使用支持的回调参数在facebook的图形API中使用JSONP。我们可以通过将参数添加到您的网址上,如

In short: JSON is not made for cross domain usage according to its same-origin policy. However the work around is to use JSONP which we can do in jQuery using the supported callback parameter in facebook's graph api. We can do so by adding the parameter onto your url like

https://graph.facebook.com/19292868552?callback=? code>

https://graph.facebook.com/19292868552?callback=?

通过使用callback =? jQuery自动更改?将json包装在函数调用中,然后允许jQuery成功解析数据。

by using the callback=? jQuery automatically changes the ? to wrap the json in a function call which then allows jQuery to parse the data successfully.

还要尝试使用$ .getJSON方法。

Also try using $.getJSON method.

这篇关于使用新的facebook图形API,ajax调用返回null(空)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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