axios-攔截器

Interceptors-攔截器
請求攔截器:在發送請求以前,能夠藉助一些函數來對請求的內容和參數作一些檢測。如有問題能夠直接取消請求。
響應攔截器:當服務器返回響應數據時,響應攔截器會在咱們拿到結果前預先處理響應數據。例如對響應數據作一些格式化處理,或者當響應失敗時,能夠作一些失敗提醒和紀錄。ios

//設置請求攔截器
axios.interceptors.request.use(function (config) {
    console.log('請求攔截器 成功')
    return config;
}, function (error) {
    console.log('請求攔截器 失敗')
    return Promise.reject(error);
});

//設置響應攔截器
axios.interceptors.response.use(function (response) {
    console.log('響應攔截器 成功')
    return response;
}, function (error) {
    console.log('響應攔截器 失敗')
    return Promise.reject(error);
});

//發送請求
axios.get('http://localhost:3000/posts').then(res=>console.log(res))
相關文章
相關標籤/搜索