[英]backbone model save not working
我嘗試使用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);
}
})
succcess: function(model, resp, opt) {
這是錯字嗎
假設success: function(model, resp, opt) {
這是一個猜測,但我認為可能是答案。 Backbone的save
委托到Backbone.sync
,Backbone使用此功能將模型讀取或保存到服務器。
骨干網使用POST,GET,DELETE和PUT; 但是某些服務器在PUT和DELETE方面存在問題。 也許您的服務器存在PUT問題,這就是為什么您的代碼無法正常工作的原因。
您可以嘗試使用Backbone.emulateHTTP = true;
。 該選項允許您使用不支持Backbone的RESTful方法的舊式Web服務器。 在同步過程中,PUT和DELETE將替換為POST。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.