設置攔截器,過濾響應數據


// 創建axios實例
const service= axios.create({

  baseURL:'/api',//配置基礎路徑
  timeout:3000
})

//請求攔截器
service.interceptors.request.use(config=>{
  //發送請求,開始進度條
  Nprogress.start()
  return config
})

// 響應攔截器
service.interceptors.response.use(
  response=>{
    Nprogress.done()
    return response.data
    // return response
   
  },

  error=>{
    Nprogress.done()
    alert('請求出錯le'+error.message || '未知錯誤')
    return new Promise(()=>{})
  }
)

發送ajax獲取的數據

 

 注;原始的response有很多數據,我們需要過濾數據,則response.data

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM