鍍金池/ 問答/HTML/ axios catch捕獲了本應(yīng)該在then里面的數(shù)據(jù)

axios catch捕獲了本應(yīng)該在then里面的數(shù)據(jù)

用axios發(fā)起請求,服務(wù)端返回了正確的數(shù)據(jù),但是數(shù)據(jù)卻被catch捕獲了,拋出的error就是本應(yīng)該在response里的數(shù)據(jù),而且用error.data賦值居然成功了。新手一個(gè),嘗試了用別人封裝好的也是一樣的結(jié)果,請教下各位大佬是什么原因?qū)е碌摹?/p>

clipboard.png

補(bǔ)充一下header截圖:
clipboard.png

clipboard.png

axios請求代碼:

    methods: {
        getData(){
            let url = 'api/public/zjinfo';
            axios.get(url).then((response)=> {
                    this.zjkdata = response.data;
                    console.log(response);
                }).catch((error)=> {
                    this.zjkdata = error.data; 
                    console.log(error);
            });
        }
    }
    

別人封裝的axios:

// axios 配置
axios.defaults.timeout = 5000;
axios.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded;charset=UTF-8';
axios.defaults.baseURL = 'http://localhost';

//POST傳參序列化
axios.interceptors.request.use((config) => {
    if(config.method  === 'post'){
        config.data = qs.stringify(config.data);
    }
    return config;
},(error) =>{
    //_.toast("錯(cuò)誤的傳參", 'fail');
    return Promise.reject(error);
});

//返回狀態(tài)判斷
axios.interceptors.response.use((res) =>{
    if(!res.data.success){
        // _.toast(res.data.msg);
        return Promise.reject(res);
    }
    return res;
}, (error) => {
    //_.toast("網(wǎng)絡(luò)異常", 'fail');
    return Promise.reject(error);
});

export function fetch(url, params) {
    return new Promise((resolve, reject) => {
        axios.post(url, params)
            .then(response => {
                resolve(response.data);
            }, err => {
                reject(err);
            })
            .catch((error) => {
               reject(error)
            })
    })
}
回答
編輯回答
懶洋洋

axios全局設(shè)置問題,已解決。

2017年4月18日 20:34