在 express js 中,我有以下代码将响应发送到 Backbone:
if (!user) {
req.session.messages = [info.message];
return res.send(400, howCanIsendTheErrorMessageAlso);
}
如何将错误消息连同错误代码一起发送?
Backbone如何接收?
有什么想法吗?
在主干中,我有以下代码:
loginModel.save({
username : obj.elEmail.val(),
password : obj.elPassword.val(),
admin : false
}, {
success: function (e) {
console.log('success');
},
error: function (e) {
console.log(e);
}
});
有什么想法吗?
最佳答案
您通过 express 方式发送:
res.send(500, { error: "hi, there was an error" });
在 Backbone 中,错误回调的参数是:模型、xhr、选项
所以你需要从错误回调中的 xhr 对象中提取错误消息,如下所示:
obj.save(
{
data:"something"
},
{
success: function(model, response, options){
console.log("worked");
},
error: function(model, xhr, options){
console.log("error", xhr.responseText);
}
}
);
关于javascript - 将自定义错误消息从 Express JS 发送到 Backbone,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20902144/