欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

axios ^0.18.0 的使用

程序员文章站 2022-07-02 17:04:55
...

拦截器的使用

    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
  })
复制代码

转载于:https://juejin.im/post/5cf5d3fd6fb9a07ee27b013a