[英]Axios request always sends back 200
我正在做出一個非常簡單的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);
我知道這行被觸發,因為登錄到瀏覽器控制台的正文顯示了相同的消息。 查看截圖。
我唯一能想到的是瀏覽器正在緩存響應?
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.' });
根據Express 4文檔-https://expressjs.com/en/4x/api.html
中間件功能是順序執行的,因此中間件包含的順序很重要。
因此,當您調用“發送”方法時,Express將HTTP正文內容寫出到響應中,這就是為什么首先需要填充標頭的原因。
像這樣先嘗試設置狀態
res.status(404).json({ message: 'Could not find user with that token or the token expired.' });
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.