gpt4 book ai didi

json - 从 Controller 读取 json 响应到 Axios Catch 部分 - Laravel Vue Axios

转载 作者:行者123 更新时间:2023-12-01 11:16:45 25 4
gpt4 key购买 nike

我有一个方法是通过axios生成的PUT来捕获vue View (Profile.vue)发送的信息,问题在于,当数据更新时(使用UserController的myProfile方法),axios捕获了方法返回json信息,并显示成功信息,但是出现错误时,Axios没有捕获错误json信息,并声称如下:

Uncaught (in promise) TypeError: Cannot read property 'status' of undefined

我知道您向我指控我在 Axios 的 catch 部分中没有信息的变量。

myProfile 代码(用户 Controller )
$profile = User::find(Auth::user()->id);
$profile->firstname = $request->firstname;
$profile->lastname = $request->lastname;
$profile->gender = $request->gender;
$profile->description = $request->description;
if($profile->update())
{
return response()->json([
'status' => 'Muy bien!',
'msg' => 'Datos actualizados correctamente.',
'cod' => 201
]);
}
else{
return response()->json([
'status' => 'Ocurrio un error!',
'msg' => 'Ocurrio un error al actualizar la información.',
'cod' => 400
]);
}

Profile.vue 的 Axios 部分
axios.put('/dashboard/profile', value)
.then((response) => {
let title = response.data.status;
let body = response.data.msg;
this.displayNotificationSuccess(title, body);
})
.catch((response) => {
let title = response.data.status;
let body = response.data.msg;
this.displayNotificationError(title,body);
})

之前说过,当 Controller 有成功时,Axios读取并显示消息json,有错误时则不显示。

我在哪里失败了 Axios 无法显示来自 Controller 的错误 json 消息?

我使用了 Laravel 5.6、Vuejs 2 和 Axios

最佳答案

catch回调,参数是错误对象,而不是 response目的。尝试这个:

axios.put('/dashboard/profile', value)
.then((response) => {
let title = response.data.status;
let body = response.data.msg;
this.displayNotificationSuccess(title, body);
})
.catch((error) => {
let title = error.response.data.status;
let body = error.response.data.msg;
this.displayNotificationError(title,body);
})

SOURCE

关于json - 从 Controller 读取 json 响应到 Axios Catch 部分 - Laravel Vue Axios,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49836036/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com