vuejs2 - 在 vue.js 中捕获 API 请求中的错误

我正在 try catch

中的错误
this.$http.post('http://127.0.0.1:8000/api/address/store',address_data,{headers: {'Authorization': 'Bearer ' + this.$auth.getToken()}}).then(response => {
                    if(response.status == 422) {
                        console.log('hello')
                        console.log(response)
                    }
            })

但我不能用这种方式捕捉。谢谢。

最佳答案

看起来您正在使用 vue-resource 来发出 ajax 请求。我只使用过 axios,但看了一下 vue-resource 的文档,您的错误回调似乎是 .then()

的第二个参数

例子

this.$http.post('/someUrl', [body], [config]).then(successCallback, errorCallback);

在你的代码中试试这个

this.$http.post('http://127.0.0.1:8000/api/address/store',address_data,
  {headers: {'Authorization': 'Bearer ' + this.$auth.getToken()}})
  .then(response => {
    // success
  }, response => {
    //error
    if(response.status == 422) {
      console.log('hello')
      console.log(response)
    }
  })

这是文档的链接:https://github.com/pagekit/vue-resource

https://stackoverflow.com/questions/49248656/

相关文章:

reactjs - 我如何将两个 Prop 从 jsx 传递到 scss

angular - 为什么在导航路线时 Angular canActivate 函数保护执行两次

apache-kafka - 一对多KStream-KTable连接

python - 类型错误 : '>' not supported between instance

python - 从python字符串中删除大写字母

c# - 如何确定在 Simple Injector 中使用哪种生活方式

php - Laravel 存储库和多个相关模型

antd - 为选择的多个组件设置默认初始值

json - 如何从 Oracle JSON 列获取数组索引?

ms-access - 从 VBA Access 中的函数返回数据类型时收到 'Invalid Us