我是 Backbone/AJAX 的新手(刚接触 Node ,我的服务器正在运行),我正在尝试使用 Backbone 的 Save 方法将我的模型“保存”给用户。我正在尝试为我的大学项目实现网络聊天。目前我想发送用户输入的用户名和密码。
我的服务器端代码:
//Module user
exports.login = function(req, res) {
console.log("login request");
console.log(req.body);
res.sendStatus(200);
};
//Main Server Script
app.post("/user/login", user.login);
我的客户端代码。
login: function(self) {
self.model.urlRoot = "/user/login";
self.model.save({}, {
success: function(model, response) {
console.log('SUCCESS:');
console.log(response);
},
error: function(model, response) {
console.log('FAIL:');
console.log(response);
}
});
}
该函数由持有登录按钮的 View 调用,并获取 View 作为参数。该 View 具有对其模型的引用。
在服务器上,我按预期获得了输入的数据,但客户端的响应给我错误:
FAIL:
Object {
readyState: 4,
getResponseHeader: .ajax/v.getResponseHeader(),
getAllResponseHeaders: .ajax/v.getAllResponseHeaders(),
setRequestHeader: .ajax/v.setRequestHeader(),
overrideMimeType: .ajax/v.overrideMimeType(),
statusCode: .ajax/v.statusCode(),
abort: .ajax/v.abort(),
state: .Deferred/d.state(),
always: .Deferred/d.always(),
then: .Deferred/d.then(),
// 11 weitere…
}
我试图自己弄清楚,但我不明白。
最佳答案
您的服务器不会发回任何东西(或 OK 字符串),响应不能被解释为有效的 JSON,然后 jQuery 认为查询失败。
在你的用户模块中,尝试
exports.login = function(req, res) {
console.log("login request");
console.log(req.body);
// send back an empty object
res.json({});
};
关于javascript - 使用 Backbone 和 Node 将模型保存到服务器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29621664/