我在获取 Backbone.js 时遇到问题。下面的代码实际上从我的服务器代码中提取了 json,但它仍然以错误函数而不是成功函数结束。
服务器代码是 PHP 设置 header 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;
});
最佳答案
好的,看来 Backbone.js 期望某些数据以特定格式返回 JSON 响应。
如果我返回 "id": "1011"
,它就可以工作,而不是在我的 json 响应中包含 id: 1011
某处有这方面的文档吗?着实让我迷惑了好久。我不断地返回我的服务器响应,认为其中有些东西不太正确,但我无法弄清楚。
这个 StackOverflow 问题和答案对我有帮助:model.fetch always going to error callback
希望这个问题和答案能够帮助其他人将来解决问题。
关于javascript - Backbone.js 获取无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14533001/