【发布时间】:2016-10-29 11:27:15
【问题描述】:
我是 Backbone 的新手。我正在尝试从骨干网中的json 获取数据。但它没有获取数据。尝试获取数据时进入错误函数。这是我的代码:
var Item = Backbone.Model.extend();
var List = Backbone.Collection.extend({
model: Item,
url: "form.json",
parse: function(response) {
return response.results;
},
sync: function(method, model, options) {
var that = this;
var params = _.extend({
type: 'GET',
dataType: 'jsonp',
url: that.url,
processData: false
}, options);
return $.ajax(params);
}
});
var ListView = Backbone.View.extend({
el: $('.deep-div'),
events: {
'click button#add': 'getPost'
},
initialize: function() {
_.bindAll(this, 'getPost');
this.collection = new List();
this.getPost();
},
getPost: function() {
var that = this;
this.collection.fetch({
success: function() {
console.log(that.collection.toJSON());
},
error: function() {
console.log('Failed to fetch!');
}
});
}
});
var listView = new ListView();
而我的json 是
{
"status": "SUCCESS",
"statusMessage": "",
"payload": [
{
"type": "text",
"label": "Name",
"currentValue": " ",
"isRequired": "true"
},
{
"type": "Date",
"label": "DOB",
"currentValue": " ",
"isRequired": "true"
}
]
}
【问题讨论】:
-
我猜是因为你的解析函数:
return response.results;试试:return response.payload; -
你能显示你得到的错误吗?
-
@Eytibi 仍然存在问题