Backbone.js在保存()后从服务器获取响应
我有一种将数据保存到codeigniter函数的模式。如果codeigniter函数有错误,它将返回有效的JSON数据。如何获取服务器在使用save()时返回的错误详细信息。我有以下代码不起作用Backbone.js在保存()后从服务器获取响应,backbone.js,Backbone.js,我有一种将数据保存到codeigniter函数的模式。如果codeigniter函数有错误,它将返回有效的JSON数据。如何获取服务器在使用save()时返回的错误详细信息。我有以下代码不起作用 this.newproject.save({ 'Objective':Objective, "Planner":Planner, "NISupervisor":NISupervisor, "SiteIDs":
this.newproject.save({
'Objective':Objective,
"Planner":Planner,
"NISupervisor":NISupervisor,
"SiteIDs":SiteIDs,
"Status":Status ,
"StartDate":StartDate,
"EndDate":EndDate,
"Details":Details,
"PrjTitle":PrjTitle
},{
success:function(model,response){
console.log(response);
}
},{
error:function(){
alert("wrong");
}
});
成功根本不起作用要保存的第二个选项是具有两个属性的对象,即成功和错误。我假设你的意思是“错误”根本不起作用,而根据你的实际问题文本,成功可以很好地发挥作用
this.newproject.save({
'Objective':Objective,
"Planner":Planner,
"NISupervisor":NISupervisor,
"SiteIDs":SiteIDs,
"Status":Status ,
"StartDate":StartDate,
"EndDate":EndDate,
"Details":Details,
"PrjTitle":PrjTitle
},{
success:function(model,response){console.log(response);},
error:function(model,response){console.log(response);}
});
错误回调还传递model和response,因此response参数可能就是您要查找的参数。要保存的第二个选项是具有两个属性的对象,即success和error。我假设你的意思是“错误”根本不起作用,而根据你的实际问题文本,成功可以很好地发挥作用
this.newproject.save({
'Objective':Objective,
"Planner":Planner,
"NISupervisor":NISupervisor,
"SiteIDs":SiteIDs,
"Status":Status ,
"StartDate":StartDate,
"EndDate":EndDate,
"Details":Details,
"PrjTitle":PrjTitle
},{
success:function(model,response){console.log(response);},
error:function(model,response){console.log(response);}
});
错误回调还传递模型和响应,因此您可能正在寻找响应参数。代码的问题是您有三个散列参数。save方法接受attrs和options作为其两个参数。因此,您的代码应该类似于以下内容:
var attrs = {
"Objective":Objective,
"Planner":Planner,
"NISupervisor":NISupervisor,
"SiteIDs":SiteIDs,
"Status":Status ,
"StartDate":StartDate,
"EndDate":EndDate,
"Details":Details,
"PrjTitle":PrjTitle
};
this.newproject.save(attrs, {
success: function(model, response) {
console.log(response);
},
error: function(model, response) {
alert('wrong');
}
});
因此,您的调用将无法附加错误函数。上面的示例应该适合您,因为它在第二个散列参数中结合了success和error函数。代码的问题是您有三个散列参数。save方法接受attrs和options作为其两个参数。因此,您的代码应该类似于以下内容:
var attrs = {
"Objective":Objective,
"Planner":Planner,
"NISupervisor":NISupervisor,
"SiteIDs":SiteIDs,
"Status":Status ,
"StartDate":StartDate,
"EndDate":EndDate,
"Details":Details,
"PrjTitle":PrjTitle
};
this.newproject.save(attrs, {
success: function(model, response) {
console.log(response);
},
error: function(model, response) {
alert('wrong');
}
});
因此,您的调用将无法附加错误函数。上面的示例应该适合您,因为它在第二个散列参数中结合了success和error函数