使用JSON获取Backbone.js集合

jes*_*lor 7 json backbone.js

我正在使用Backbone.js和Phil Sturgeon的CI休息服务器(绝对值得推荐的AMAZING TOOL).

这是我的页面:http://interr0bang.net/7357/fetch/.它非常基础,模型(Event),集合(Events)和视图(EventView).该集合位于http://api.interr0bang.net/calendar/events,并返回已使用jsonformatter.curiousconcept.com验证的JSON数组.

这是代码:

$(function(){
    var Event = Backbone.Model.extend();
    var Events = Backbone.Collection.extend({
        model: Event,
        url: 'http://api.interr0bang.net/calendar/events',

    });
    var EventView = Backbone.View.extend({
        initialize: function(){
            _.bindAll(this, "render","count");
            this.collection = new Events();
            this.collection.bind("change",this.count);
            this.collection.fetch();
            this.counter = this.collection.length;
            this.render();
        },
        render: function(){
            this.el.html(this.counter);
        },
        count: function(){
            this.counter = this.collection.length;
        }
    });
    eventView = new EventView({el:$('#collection')});
});
Run Code Online (Sandbox Code Playgroud)

视图呈现正常,但它始终显示0,Firebug显示GET请求,状态为200 OK,但响应正文为空...为什么这不起作用?

Elf*_*erg 6

您有配置问题.如果您查看浏览器,它会报告:

XMLHttpRequest cannot load http://api.interr0bang.net/calendar/events. Origin     
http://interr0bang.net is not allowed by Access-Control-Allow-Origin.
Run Code Online (Sandbox Code Playgroud)