Axios: My interceptor can't intercept response

Created on 10 Jan 2017  Â·  1Comment  Â·  Source: axios/axios

I define an Axios instance, my post request still can get response object even if i return false when data.code == '10002'

import Axios from 'axios'

var axios = Axios.create({
    headers: {
        'Content-Type': 'application/json'
    }
})

axios.interceptors.request.use(config=>{
    config.headers['x-jwt-token'] = localStorage.getItem('token')
    return config
}, error=>{
    return Promise.reject(error)
})

axios.interceptors.response.use(function(response) {
    if(response.data.code == '10002'){
        //call RPC 过程中缺少字段
        app.$message.error('缺少字段' + response.data.message)
        return
    }
    return response;
}, function(error) {
    // Do something with response error
    return Promise.reject(error);
});


Vue.prototype.$http = axios

Most helpful comment

You need to throw an error or return a rejected promise from your interceptor.

Also, you don't need to define the second parameter of use if it just re-throws the error (not passing a second parameters has the same effect).

>All comments

You need to throw an error or return a rejected promise from your interceptor.

Also, you don't need to define the second parameter of use if it just re-throws the error (not passing a second parameters has the same effect).

Was this page helpful?
0 / 5 - 0 ratings

Related issues

jdpagley picture jdpagley  Â·  3Comments

Shu-Ji picture Shu-Ji  Â·  3Comments

Spartano picture Spartano  Â·  3Comments

altruisticsoftware picture altruisticsoftware  Â·  3Comments

samayo picture samayo  Â·  3Comments