axios ^0.18.0 的使用

350 阅读1分钟

拦截器的使用

    axios .interceptors.response.use(response => {
        // 与后台约定的返回码
        if (response.data.retCode == 3001) {
            window.location.href = response.data.redirectUrl;
        }
        return response; // 响应参数
    }, (responseError) => {
        console.log("responseError", responseError);
    })

axios.get()

    axios.get(url, {params: query}).then(response => {
        ....// business logical
    })

axios.post()

    axios.post(url, {params: query}).then(response => {
        .../ business logical
    })

axios

  // get
  axios({
      url: `${interfaceURL}`,
      method: `get`,
      params: {
        query1: ...  
      },
      headers: {
        `Content-Type`:`application/x-www-form-urlencoded`
      }
  }).then(res => {
      // business logical
  })
  let query = {...};
  //post
  axios({
    url: `${interfaceURL}`,
    method: `post`,
    data: query, 
    transformRequest: [
      function(data) {
        return JSON.strigify(data)
      }
    ],
    headers: {
     `Content-Type`: `application/json; charset=utf-8`    
    }
  }).then(res => {
    // business logical
  })