【问题标题】:Axios catch error returns JavaScript error not server responseAxios 捕获错误返回 JavaScript 错误而不是服务器响应
【发布时间】:2019-10-07 17:52:32
【问题描述】:

我正在尝试从服务器捕获验证错误。

代码:

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

控制台日志输出

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

网络标签输出

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

有没有办法改变它或者我做错了什么?

谢谢!

【问题讨论】:

  • 我无法理解您的问题..?你总是正确地处理异步调用异常

标签: javascript laravel vue.js axios


【解决方案1】:

谷歌搜索了一下,我找到了这张票:

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

建议使用:

console.log(error.response)

而不是

console.log(error)

编辑 1:

进一步阅读,显然这可能是 CORS 问题的征兆,请参阅 thisthis

网络选项卡上的请求看起来好像成功了,但是,当它被 Axios 处理时,会返回一个Network Error

【讨论】:

  • 我无法从该票证中获得任何解决方法
  • 好吧 - 当你使用上面的代码时你的输出会发生什么?
  • 我在使用 console.log(error.response) 时在日志中看到了 "undefined" 消息
  • @SanjeevanRaja - 我在上面添加了一个额外的答案,也许调查一下?
  • 是的。你是对的@n15m0-jk。我解决了我的问题。这是一个CORS问题。非常感谢
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-02-27
  • 2021-05-21
  • 2020-01-27
  • 2021-02-07
  • 2012-05-05
  • 2020-01-17
相关资源
最近更新 更多