从控制器读取 json 响应到 Axios Catch 部分 - Laravel Vue Axios

Fel*_*ipe 4 json laravel laravel-5 axios vuejs2

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

Uncaught (in promise) TypeError: Cannot read property 'status' of undefined
Run Code Online (Sandbox Code Playgroud)

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

myProfile 代码(用户控制器)

$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
            ]);
}
Run Code Online (Sandbox Code Playgroud)

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);
        })
Run Code Online (Sandbox Code Playgroud)

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

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

我使用了 Laravel 5.6、Vuejs 2 和 Axios

apr*_*ja1 5

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);
    })
Run Code Online (Sandbox Code Playgroud)

来源