尝试将backbone.js 与 Facebook json feed 一起使用时出现语法错误
我有一个超级简单的 Backbone 模型/集合,它包含一个 facebook feed。
window.Story = Backbone.Model.extend({});
window.Stories = Backbone.Collection.extend({
model: Story,
url: 'https://www.facebook.com/feeds/page.php?id=186424828078649&format=json&callback=?',
parse: function(response) {
console.log(response);
return response.entries;
}
});
stories = new Stories();
stories.fetch();
这会导致 Chrome 中出现语法异常(“意外标记:”),或 Firefox 中出现无效标签 “title”:{
。
Chrome 消息有点神秘,但 Firefox 消息似乎表明正在返回 JSON 响应。 parse() 函数永远不会被调用(据我所知,我设置了一个断点并添加了一个 console.log 语句,但我没有看到任何内容),所以有什么否则 Backbone 需要知道才能解析 JSON?
我的第一个想法是这可能是跨域问题,但我使用 &callback=?
这应该确保 $.ajax 使用 jsonp。我敢打赌这是有效的,因为 Firefox 正确地将第二行识别为 "title": {
。
有什么想法吗?
PS,如果您想测试一下,jsfiddle 位于 http://jsfiddle.net/KcE9L/ 。
I have a super simple Backbone model/collection that wraps around a facebook feed.
window.Story = Backbone.Model.extend({});
window.Stories = Backbone.Collection.extend({
model: Story,
url: 'https://www.facebook.com/feeds/page.php?id=186424828078649&format=json&callback=?',
parse: function(response) {
console.log(response);
return response.entries;
}
});
stories = new Stories();
stories.fetch();
This results in a syntax exception in Chrome ("Unexpected token :"), or in Firefox, an invalid label "title": {
.
The Chrome message is a little cryptic, but the Firefox message seems to indicate that the JSON response is being returned. the parse()
function is never getting called (as far as I can tell, I've set a breakpoint and added a console.log statement and I'm not seeing anything), so is there anything else that Backbone needs to know in order to parse JSON?
My first thought was that it might be a Cross-Domain issue, but I'm using &callback=?
which should make sure that $.ajax uses jsonp. I'm betting that this is working, because Firefox is identifying the 2nd line correctly as "title": {
.
Any ideas?
PS, jsfiddle is at http://jsfiddle.net/KcE9L/ if you'd like to test things out.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
经过一番深度调试后我发现了这一点。这是因为 Facebook 忽略/不支持 JSONP,给我留下了这个问题: Jquery 成功函数未使用 JSONP 触发。现在我需要弄清楚如何让 CORS 与 Facebook 一起工作,所以如果有人可以帮助我实现这些目标,我会很好奇。
I figured it out after some deep debugging. It's because Facebook ignores/doesn't support JSONP, and leaves me with this problem: Jquery success function not firing using JSONP. Now I need to figure out how to get CORS working with Facebook, so if anybody can help me along those lines, I'd be curious.