简体   繁体   中英

Axios catch error returns JavaScript error not server response

Im trying to catch validation errors from the server.

Code:

axios.post('https://myBackendIp.com/api/teacher/time/create',Data)
.then(res=>{
       console.log(res)
 }).catch(error=>{
       console.log(error)
 })

Console log output

Error: Network Error
at createError (createError.js:16)
at handleError (xhr.js:87)

Network tab output

{"error":{"message":"Check your date field","status_code":500}}

Is there a way to change that or I'm doing something wrong?

Thanks!

After Googling a little, I found this ticket:

https://github.com/axios/axios/issues/960

It recommends using:

console.log(error.response)

instead of

console.log(error)

Edit 1:

Did some further reading, apparently this could be a symptom of a CORS issue, see this and this .

The request on the network tab might seem like it's succeeding, however, when it's processed by Axios, a Network Error is returned.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM