我正在更新一个表单,我想在服务器上发出一个更新请求,我的模型是:
var CampaignEditModel = Backbone.Model.extend({
urlRoot:"http://localhost:3033/campaign/update/",
url : function(){
var url = this.urlRoot + this.id;
return url;
},
idAttribute: "_id",
defaults:{
"id":null ,
"Name" :""
}
});
渲染函数在这里被调用:
$contents.empty().append(new EditView({model:editCampaigns}).render({id:id}).el);
和渲染功能是:
render: function(options){
this.$el.append( _.template(EditTemplate));
this.model.set({"id":options.id})
console.log(this.model.get("id"));
this._modelBinder.bind(this.model, this.el);
return this;
},
events: {
'click .saveCampaign ': 'save'
},
save:function(){
this.model.set({
"Name" :$('#edname').val(),
});
this.model.save(null, {success: function(data){
console.log("data:" + data);
require(['campaignroute'],function(routes){
var router = routes.pageRouter;
router.navigate('gridView', {trigger: true});
});
}});
return false;
}
问题是即使我在模型中设置了一个 id 仍然在调用 save 方法时请求像这样
http://localhost:3033/campaign/update/undefined
和控制台显示错误:
Failed to load resource: the server responded with a status of 404 (Not Found)
如何解决这个问题呢?