Vuejs2 捕获vue.js中API请求中的错误

Vuejs2 捕获vue.js中API请求中的错误,vuejs2,Vuejs2,我正试图抓住这个错误 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.

我正试图抓住这个错误

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资源发出ajax请求。我只使用了axios,但浏览一下vue资源的文档,您的错误回调似乎是
的第二个参数。then()

例子
this.$http.post('/someUrl',[body],[config])。然后(successCallback,errorCallback)

在你的代码中试试这个
这里是文档的链接:

看起来您正在使用vue资源发出ajax请求。我只使用了axios,但浏览一下vue资源的文档,您的错误回调似乎是
的第二个参数。then()

例子
this.$http.post('/someUrl',[body],[config])。然后(successCallback,errorCallback)

在你的代码中试试这个
这里是文档的链接:

关于stackoverflow的回答将消除您的疑虑-关于stackoverflow的回答将消除您的疑虑-
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)
    }
  })