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;
});