[英]Axios catch error from Response JSON is undefined
我正在從我的 php 返回一個自定義響應,但我不知道如何將這些響應用於我的頁面。 我試圖使用 console.log 查看狀態和 msg 但沒有運氣。
if ($message->send()){
return response()->json([
'status' => 'success',
'msg' => 'Message send =)'
],201);
}else{
return response()->json([
'status' => 'error',
'msg' => 'Message did not send =('
],422);
}
在我的腳本中
axios.post('url',params)
.then(function(){
console.log('send');
})
.catch(function(error){
if (error.response){
console.log(error.response.data.msg);
}else if(error.request){
console.log(error.response.data.msg)
}else{
console.log(error.response.data.msg);
}
});
我想獲取狀態和msg但 console.log(error.response.data.msg) 是undefined 。 我是在做錯誤的返回還是錯誤的 console.log() ?
感謝您的幫助。
我修好了它! 我將函數(錯誤)替換為函數(響應)。 像這樣
.then(function(response){
console.log('response.data.msg');
console.log('response.data.status');
})
.catch(function(response){
console.log('response.data.msg');
console.log('response.data.status');
});
遵循錯誤響應格式,您需要使用 error.response.message 來獲取錯誤的詳細信息。 至少,嘗試通過console.log(error.response)
了解發生了什么。 希望這有幫助!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.