0

我正在尝试一个简单的backbone.js 应用程序。并且在进行集合获取时出错并且无法弄清楚原因。任何要检查的指针都会有所帮助。

1-我的后端是基于java的。

2-这是我的模型和收藏

var lesson = Backbone.Model.extend
({
defaults : 
    {
        lessonId: '',
        lessonName: '',
        subject: '',
        textBook: '',
    },

    initialize: function()
    {}
   });

var lessonsCollection = Backbone.Collection.extend
({
model: lesson,
url: "/lessons?subject=Math&class=5",

initialize : function(){
    //var items = this.fetch();
    //console.log(items);
}

});

3-服务器对数据请求产生的响应是:

[
{"lessonId":"00000001","lessonName":"DemoFractions","subject":"Math","textBook":"Null"},
{"lessonId":"00000002","lessonName":"test ","subject":"Math","textBook":"test"}
]

4-我的 fetch 调用代码看起来像

var coll = new lessonsCollection();
coll.fetch(
        {
        success: function(){
           console.log("got data");
           } ,

         error: function(){
           console.log("error in getting data through rest");
           }
        }
    );

但是,每次 fetch 调用都会导致错误。我的代码中缺少什么?

已编辑:根据建议,我现在正在记录响应。我的代码看起来像

     coll = new lessonsCollection; 
 coll.fetch(
         {    
               success: function(){
                     console.log("got data");
                     self.render() ; 
               } ,

       error: function(collection, response){
             console.log("error in getting data through rest");
             console.log("response text got ",response.responseText);
             console.log(response);
           }
         }     
        );  

控制台日志对我来说就像一个 js 代码。

通过休息响应文本获取数据时出错了
({readyState:0, getResponseHeader:(function (e){var t;if(2===b){if(!c){c={};while(t=Tn.exec(a))c[ t[1].toLowerCase()]=t[2]}t=c[e.toLowerCase()]}return null==t?null:t}), getAllResponseHeaders:(function (){return 2=== b?a:null}), setRequestHeader:(function (e,t){var n=e.toLowerCase();return b||(e=v[n]=v[n]||e,y[e ]=t),this}), overrideMimeType:(function (e){return b||(p.mimeType=e),this}), statusCode:(function (e){var t;if(e)if( 2>b)for(t in e)m[t]=[m[t],e[t]];else C.always(e[C.status]);return this}), abort:(function ( e){var t=e||w;return u&&u.abort(t),k(0,t),this}), state:(function (){return n}), always:(function (){return i.done(arguments).fail(arguments),this}), then:(function (){var e=arguments;return x.Deferred(function(n){x.each(t,function(t,o) {var a=o[0],s=x.isFunction(e[t])&&e[t];i[o[1]](function(){var e=s&&s.apply(this,arguments);e&&x.isFunction(e.promise)?e.promise().done(n.resolve).fail(n.reject).progress(n.notify):na+"With"})} ),e=null}).promise()}), promise:(function (e){return null!=e?x.extend(e,r):r}), pipe:(function (){var e =arguments;return x.Deferred(function(n){x.each(t,function(t,o){var a=o[0],s=x.isFunction(e[t])&&e[t]; i[o[1]](function(){var e=s&&s.apply(this,arguments);e&&x.isFunction(e.promise)?e.promise().done(n.resolve).fail(n. reject).progress(n.notify):na+"With"})}),e=null}).promise()}), done:(function (){if(l){var t=l.length; (函数 i(t){x.each(t,function(t,n){var r=x.type(n);"function"===r?e.unique&&p.has(n)||l. push(n):n&&n.length&&"string"!==r&&i(n)})})(参数),n?o=l.length:r&&(s=t,c(r))}return this}) , 失败:(function (){if(l){var t=l.length;(function i(t){x.each(t,function(t,n){var r=x.type(n);"function" ===r?e.unique&&p.has(n)||l.push(n):n&&n.length&&"string"!==r&&i(n)})})(参数),n?o=l.length :r&&(s=t,c(r))}return this}), 进度:(function (){if(l){var t=l.length;(function i(t){x.each(t, function(t,n){var r=x.type(n);"function"===r?e.unique&&p.has(n)||l.push(n):n&&n.length&&"string"!= =r&&i(n)})})(参数),n?o=l.length:r&&(s=t,c(r))}return this}), complete:(function (){if(l){ var t=l.length;(函数 i(t){x.each(t,function(t,n){var r=x.type(n);"function"===r?e.unique&&p.has (n)||l.push(n):n&&n.length&&"string"!==r&&i(n)})})(参数),n?o=l.length:r&&(s=t,c(r))}return this}), 成功:(function (){if(l){var t=l.length;(function i(t){x.each(t,function(t,n){ var r=x.type(n);"function"===r?e.unique&&p.has(n)||l.push(n):n&&n.length&&"string"!==r&&i(n)}) })(参数),n?o=l.length:r&&(s=t,c(r))}return this}), error:(function (){if(l){var t=l.length; (函数 i(t){x.each(t,function(t,n){var r=x.type(n);"function"===r?e.unique&&p.has(n)||l. push(n):n&&n.length&&"string"!==r&&i(n)})})(参数),n?o=l.length:r&&(s=t,c(r))}return this}) , responseText:"", status:0, statusText:"error"})"string"!==r&&i(n)})})(参数),n?o=l.length:r&&(s=t,c(r))}return this}), error:(function (){ if(l){var t=l.length;(函数 i(t){x.each(t,function(t,n){var r=x.type(n);"function"===r? e.unique&&p.has(n)||l.push(n):n&&n.length&&"string"!==r&&i(n)})})(参数),n?o=l.length:r&&(s= t,c(r))}return this}), responseText:"", status:0, statusText:"error"})"string"!==r&&i(n)})})(参数),n?o=l.length:r&&(s=t,c(r))}return this}), error:(function (){ if(l){var t=l.length;(函数 i(t){x.each(t,function(t,n){var r=x.type(n);"function"===r? e.unique&&p.has(n)||l.push(n):n&&n.length&&"string"!==r&&i(n)})})(参数),n?o=l.length:r&&(s= t,c(r))}return this}), responseText:"", status:0, statusText:"error"})“”,状态:0,状态文本:“错误”})“”,状态:0,状态文本:“错误”})

谢谢

4

1 回答 1

0

好吧,您在响应中返回的具体错误对我们查看确实很有帮助,但您至少需要在idAttribute您的Lesson模型上进行设置。

var Lesson = Backbone.Model.extend({
  idAttribute: 'lessonId',
  defaults: {
    lessonId: '',
    lessonName: '',
    subject: '',
    textBook: ''
  }
});

看看能不能解决。否则,记录响应:

   error: function(collection, response){
     console.log("error in getting data through rest", response);
   }
于 2013-08-07T19:31:16.583 回答