最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to display response error message with axios - Stack Overflow

programmeradmin6浏览0评论

Been looking to achieve this few months now but now it's needed. I need to display the error message, set on server, about what went wrong:

Sever endpoint

abort(406, 'Foo bar!')

Web:

[...]
 .catch(e => {
  console.log(e.response.data) // does not work
 })

In chrome dev tools, under preview, I see the message Foo bar!. How to get this message on my console log?

This endpoint will have different abort messages based on actions so I do not want to set a static error message on web for the error.

Chrome dev tools message:

Been looking to achieve this few months now but now it's needed. I need to display the error message, set on server, about what went wrong:

Sever endpoint

abort(406, 'Foo bar!')

Web:

[...]
 .catch(e => {
  console.log(e.response.data) // does not work
 })

In chrome dev tools, under preview, I see the message Foo bar!. How to get this message on my console log?

This endpoint will have different abort messages based on actions so I do not want to set a static error message on web for the error.

Chrome dev tools message:

Share Improve this question edited Jun 6, 2017 at 9:52 Sylar asked Jun 6, 2017 at 9:09 SylarSylar 12.1k27 gold badges103 silver badges179 bronze badges 1
  • 1 Possible duplicate of How can I get the status code from an http error in Axios? – Sylar Commented Jun 9, 2017 at 16:19
Add a comment  | 

2 Answers 2

Reset to default 21

This should work : console.log(e.response.data.message). I think this is what you looking for.

This is for this type of request:

axios.post(
    url,
    {},
    })
    .then(function (response) {
      console.log(response);
    })
    .catch(function (error) {
      console.log(error.response.data.message);
    });

This may help

catch (error) {
            if (error.response) {
                console.log(error.response)
                console.log(error.response?.data?.message)
                return 
            }
        }
发布评论

评论列表(0)

  1. 暂无评论