我正在尝试一个简单的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-我的获取调用代码看起来像
var coll = new lessonsCollection();
coll.fetch(
{
success: function(){
console.log("got data");
} ,
error: function(){
console.log("error in getting data through rest");
}
}
);
但每次获取调用都会导致错误。我的代码中缺少什么?
编辑:基于建议我现在正在记录响应。我的代码看起来像
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(if) 2> b)for(t in e)m [t] = [m [t],e [t]];否则C.always(e [C.status]);返回此}},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(这一点,参数); E&安培;&放大器;。x.isFunction(e.promise)e.promise()来完成(n.resolve).fail(n.reject).progress(n.notify) :NA + “与”})})中,e = NULL})承诺() }),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 .reogress).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&安培;&安培; “字符串” ==的R&&安培; I(N)})})(参数)中,n O = l.length!?: r&&(s = t,c(r))}返回此}},失败:(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&安培;&安培; “字符串” ==的R&&安培; I(N)})})(参数)中,n O = l.length:!?的R&及(S = T,C(R )}}}返回此}},进度:( 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))}返回此}), complete:(function(){if(l){var t = l.length;(function i(t){x.each(t,function(t,n){var r = x.type(n);“功能 “=== [R e.unique和放大器;&安培; p.has(N)|| l.push(N):N'放大器;&安培; n.length和放大器;&安培;!” 串“==的R&&安培; I( n)})})(arguments),n?o = l.length:r&&(s = t,c(r))}返回此}},success:(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和放大器;&安培; “串” ==的R&&安培; I(N)})})(参数),N!? o = l.length:r&&(s = t,c(r))}返回此}},错误:(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&安培;&安培; “字符串” ==的R&&安培; I(N)})})(参数)中,n O = l.length:?的R&&安培;(S = t,c(r))}返回此}), responseText:“”,status:0,statusText:“error”})
感谢
答案 0 :(得分: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);
}