我有以下功能(最终)将用于保存记录
exports.save = function(req,res) {
var userProfile = new UserProfile();
try {
var i = 0;
var startPos = 0;
var nameLength = 0;
console.log("Saving profile");
strPos = req.body.displayName.indexOf(' ');
nameLength = req.body.displayName.length;
userProfile._id = req.body.id;
userProfile.email = req.body.emailAddress;
if (nameLength > 0) {
userProfile.givenName = req.body.displayName.substring(0, strPos);
userProfile.familyName = req.body.displayName.substring(nameLength,strPos+1);
}
else {
userProfile.givenName = userProfile.givenName;
userProfile.familyName = "";
}
userProfile.displayName = req.body.displayName;
console.log("well that bit worked - now hows about returning to the calling page");
// processed OK
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('OK');
}
catch(e) {
console.log("Oh dear - something broke");
console.log(e.message);
}
};
这将通过Node中的以下行路由到。
app.post('/saveProfile',profile.save);
并通过以下jquery代码从网页调用。
$.ajax({
type: 'POST',
url: '/saveProfile',
data: JSON.stringify(userProfile),
contentType: 'application/json; charset = utf-8',
dataType: 'json',
success: function(data) {
alert('OK!')
},
error: function (err) {
alert('Returned error ' + err);
}
});
根据Fiddler的说法,我收到了以下回复
HTTP / 1.1 200好的 X-Powered-By:快递 Content-Type:text / plain 日期:太阳,2014年3月30日14:56:02 GMT 连接:保持活力 Transfer-Encoding:chunked
2 好 0
但是始终会调用错误 - 不会返回错误消息。我已经通过向控制台回显消息来检查响应头的设置。
那么我在从保存功能返回时做错了什么,这意味着调用页面认为发生了错误,或者至少认为它没有成功。我试过从Chrome和IE调用。
提前致谢