WinJS.xhr检索Facebook JSON无法正常工作

时间:2013-03-17 03:55:45

标签: javascript json facebook-graph-api windows-8

我一直在玩Windows 8 VS 2012开发,但我陷入困境,无法弄清楚我的错误在哪里。使用Twitter API时,如下所示,一切都按预期工作:

WinJS.UI.Pages.define("/pages/home/home.html", {
    ready: function (element, options) {
        WinJS.xhr({ url: "http://search.twitter.com/search.json?q=%23Windows8&rpp=5" }).then(
            function (response) {
                var json = JSON.parse(response.responseText);
                var list = new WinJS.Binding.List(json.results);

                // connect list to feedView1 element
                feedView1.winControl.itemDataSource = list.dataSource;
            },
            function (error) { console.log("Can't connect to FB: " + error)
    );

});

但是,当我使用这样的访问令牌将URL更改为Facebook Graph链接时(我确实包含了一个有效的令牌):

https://graph.facebook.com/me/home?limit=5&access_token=

我没有回应。我跑了两个并添加了console.log支票:

WinJS.UI.Pages.define("/pages/home/home.html", {
    ready: function (element, options) {
        WinJS.xhr({ url: "http://search.twitter.com/search.json?q=%23Windows8&rpp=5" }).then(
            function (response) {
                var json = JSON.parse(response.responseText);
                console.log("JSON TYPE: " + json);
                console.log("RESULTS: " + json.results);
                var list = new WinJS.Binding.List(json.results);

                // connect list to feedView1 element
                feedView1.winControl.itemDataSource = list.dataSource;
            },
            function (error) { console.log("Can't connect to FB: " + error)
    );

});

这表明Twitter输出:

JSON TYPE: [object Object]
RESULTS: [object Object],[object Object],[object Object],[object Object],[object Object]

但Facebook输出:

JSON TYPE: [object Object]
RESULTS: undefined

FB JSON有预先格式化导致问题吗?或者FB如何嵌套JSON数据集?除了我所做的以外,我无法弄清楚如何在VS内部进行故障排除。

注意:我试图在纯javascript中执行此操作,而无需添加JQuery。

编辑1:

我跑了

console.log("RESPONSE TYPE: " + response.getAllResponseHeaders()); 

并发现了一个问题:

TWIT RESPONSE TYPE: Cache-Control: max-age=15, must-revalidate, max-age=300
Expires: Sun, 17 Mar 2013 08:28:13 GMT
Content-Type: application/json;charset=utf-8
X-Transaction: 4e16996bd3651e20
X-Frame-Options: SAMEORIGIN
X-Content-Type-Options: nosniff
Status: 200 OK
Date: Sun, 17 Mar 2013 08:23:13 GMT
X-Varnish: 2456909043
Age: 0
Via: 1.1 varnish
Server: tfe

而FB有:

FB RESPONSE TYPE: Access-Control-Allow-Origin: *
Cache-Control: private, no-cache, no-store, must-revalidate
Content-Type: text/javascript; charset=UTF-8
ETag: "d8346604dd33e1252132b3e2e13661e0c900f6c2"
Expires: Sat, 01 Jan 2000 00:00:00 GMT
Pragma: no-cache
X-FB-Rev: 758528
X-UA-Compatible: IE=edge,chrome=1
X-FB-Debug: dI/RKE6LTHWk0m0JTLutm2rLpzTQ5zd96PHK267eHfQ=
Date: Sun, 17 Mar 2013 08:23:13 GMT
Connection: keep-alive

FB正在提供text / javascript Content-Type。所以看起来像json,但实际上并不是json。

不确定如何解决这个问题。

0 个答案:

没有答案