我无法让 Backbone.js 获取工作。以下代码实际上从我的服务器代码中提取了 json,但它仍然以错误函数结束,而不是成功函数。
服务器代码是 PHP 设置标头 application/json 并返回有效的 json。
当 Javascript 运行时,我在控制台日志中得到以下信息。
{"id":"1011"} client.js:22
{"readyState":4,"responseText":"{id: 1011,address: \"123 Main St.\"}","status":200,"statusText":"OK"} client.js:23
{"parse":true,"emulateHTTP":false,"emulateJSON":false,"xhr":{"readyState":4,"responseText":"{id: 1011,address: \"123 Main St.\"}","status":200,"statusText":"OK"}} client.js:24
$(function() {
var Property = Backbone.Model.extend({
urlRoot: 'property'
});
var AppView = Backbone.View.extend({
el: $('#content'),
error: function(m, xhr, opts) {
console.log(JSON.stringify(m));
console.log(JSON.stringify(xhr));
console.log(JSON.stringify(opts));
},
render: function() {
alert("Success");
},
initialize: function() {
var options = {};
options.success = this.render;
options.error = this.error;
var property = new Property({id: "1011"});
property.fetch(options);
}
});
var appview = new AppView;
});