我尝试使用 Backbone.js 模型与服务器同步数据。但是保存方法不起作用。任何人都可以帮我找出原因吗?
以下代码有效(不使用 Backbone 模型):
var newComment = {
user: user,
createdDate: date,
content: text
}
//TODO send ajax post request to record the data
$.ajax({
url: "comment.php",
context: document.body,
data: newComment,
type: 'POST'
}).done(function(resp) {
console.log("resp", resp);
});
代码不起作用:
var Comment = Backbone.Model.extend({
urlRoot: 'comment.php'
});
var comment = new Comment();
comment.save({content: text, dsm_line_item_id: "49934380"}, {
succcess: function(model, resp, opt) {
console.log("successfully saved" + resp);
},
error: function(model, resp, opt) {
console.log("error", resp);
}
})