如何在axios中打印laravel的5.5验证响应

时间:2017-09-21 11:23:20

标签: php ajax laravel axios

我的代码正在使用laravel 5.4,升级后无法运行。然后我试图在网络中看到返回的响应对象。

这就是我所看到的

{message: "The given data was invalid.",…}
  errors:{userName: ["The user name has already been taken."], email: ["The 
     email has already been taken."]}

在我的axios中,我正在尝试控制消息"用户名已被采取"和"电子邮件已被拍摄。"

但我显示未定义。这是我的代码。

.catch(function (error) {
       // This shows undefined. 
       console.log(error.errors]);
  });

完整对象

{
"message": "The given data was invalid.",
"errors": {
    "userName": [
        "The user name field is required."
    ],
    "email": [
        "Email is required!"
    ],
    "password": [
        "The password field is required."
    ],
    "lastName": [
        "The last name field is required."
    ],
    "userType": [
        "The user type field is required."
    ]
} 
}

3 个答案:

答案 0 :(得分:1)

Axios传递的响应值将包含data字段,其中包含您的值。所以你的代码需要

.catch(function (error) {
   // This shows undefined. 
   console.log(error.data.errors);
});

答案 1 :(得分:1)

.then中查看您的回复,就像这样

.then(function(response) {
    console.log(response.data.errors);
});

答案 2 :(得分:0)

感谢您帮助大家。我用你的帮助解决了它。我是这样做的。

 .catch(error => {
     console.log(error.response.data.errors.email[0])
  });