我正在做一个非常简单的PUT请求(见下文)。出于某种原因,无论我从快递服务器发回的响应代码,axios都会看到200.在我的快速日志中,它显示正确的响应:
PUT /v1/org/password/reset 404 339.841 ms - 71
这是发送响应服务器端的代码行:
res.json({ message: 'Could not find user with that token or the token expired.' }).status(404);
我知道这条线正在被触发,因为登录到浏览器控制台的主体显示相同的消息。见screenshot.
我唯一能想到的是浏览器缓存响应?
axios.put(`http://localhost:3000/v1/org/password/reset`, {
password: "example",
token: "19u8e8j2039d3d32blahblah"
})
.then(function (response) {
if(response.status == 200){
console.log("success",response)
//handle success here
}
})
.catch(function (error) {
console.log("error", error);
//handle error here
});
尝试首先设置状态
res.status(404).json({ message: 'Could not find user with that token or the token expired.' });
问题出在后端。尝试以这种方式返回响应:
res.status(404).json({ message: 'Could not find user with that token or the token expired.' });
根据Express 4文档 - https://expressjs.com/en/4x/api.html
中间件功能是顺序执行的,因此中间件包含的顺序很重要。
因此,当您调用“send”方法时,Express会将http正文内容写入响应,这就是首先需要填充标题的原因。