如何在 Node.js express 和 jQuery 客户端之间获取正确的 HTTP POST 状态码
How to get the correct HTTP POST status code between Node.js express and jQuery client
在 Node.js 应用程序 运行 Express 中,我有一个路由设置来接受 POST 请求并创建数据库对象。
app.post('/', function(req, res) {
// create object in DB
res.send('');
res.status(201).end();
});
我理解 201 status code 表示对象创建成功。我希望在客户端的 jQuery POST 中收到 201 状态:
$.post('/', sendData).done(function(receiveData, status, xhr) {
alert(xhr.status); // returns 200 status
});
但是,我收到的是 200 状态,而不是我尝试发送的 201。
我可以更改什么以在客户端接收我尝试从服务器发送的 201 状态代码?
来自docs
尝试在发送前设置状态:
app.post('/', function(req, res) {
// create object in DB
res.status(201).send('');
});
在 Node.js 应用程序 运行 Express 中,我有一个路由设置来接受 POST 请求并创建数据库对象。
app.post('/', function(req, res) {
// create object in DB
res.send('');
res.status(201).end();
});
我理解 201 status code 表示对象创建成功。我希望在客户端的 jQuery POST 中收到 201 状态:
$.post('/', sendData).done(function(receiveData, status, xhr) {
alert(xhr.status); // returns 200 status
});
但是,我收到的是 200 状态,而不是我尝试发送的 201。
我可以更改什么以在客户端接收我尝试从服务器发送的 201 状态代码?
来自docs
尝试在发送前设置状态:
app.post('/', function(req, res) {
// create object in DB
res.status(201).send('');
});