// Request configuration of the Java interface
import axios from "axios"
import { Message } from "element-ui"
import qs from "qs"

const instance = axios.create({
  timeout: 10000});// Request interception
instance.interceptors.request.use(config= > {
  // You can add a get local token here to add in the header
  //const token = sessionStorage.getItem("token");
  //token && (config.headers.Authorization = token);
  return config;
}, err= > {
  return Promise.reject(err);
})

// Response interception
instance.interceptors.response.use((response) = > {
  const res = response.data
  // If the status code of the server is not 200, the request is abnormal and an error message is displayed.
  if(res.code ! = =200) {
    // You can jump to the login location according to the return value of 401. At the same time, you need to consider that there is no code and other fields when the return is binary stream
    Message({
      message: res.status_desc,
      type: 'error'.duration: 2 * 1000
    })
    return Promise.reject(new Error(res.status_desc || 'Error'))}else {
    return res
  }
}, (err) = > {
  Message({
    message: err.message,
    type: 'error'.duration: 2 * 1000
  })
  return Promise.reject(err)
})

let requestJava = {
  get: function(url, params = {}) {
    return new Promise((resolve, reject) = > {
      instance({
        url,
        method: 'get',
        params,
        paramsSerializer: function(params) {
          return qs.stringify(params)[1,2,3] is received by the background
        }
      }).then(response= > {
        resolve(response)
      }).catch(error= > {
        reject(error)
      })
    })
  },
  post: function(url, data) { / / used to application/json
    return new Promise((resolve, reject) = > {
      instance({
        url,
        method: 'post',
        data,
        transformRequest: function(data) {
          return JSON.stringify(data)// the background receives the form 1,2,3
        },
        headers: {
          "Content-Type": "application/json; charset=UTF-8",
        }
      }).then(response= > {
        resolve(response)
      }).catch(error= > {
        reject(error)
      })
    })
  },
  post1: function(url, data) { / / used in the application/x - WWW - form - urlencode
    return new Promise((resolve, reject) = > {
      instance({
        url,
        method: 'post',
        data,
        transformRequest: function(data) {
          return qs.stringify(data)
        },
      }).then(response= > {
        resolve(response)
      }).catch(error= > {
        reject(error)
      })
    })
  }
}

export default requestJava
Copy the code

Extension:

  • Sending requests for receiving binary streams: used to process binary stream requests
  • What is qs.stringify()? Serialize the object as a URL, concatenated with &, where the Stringify method converts a JSON object directly to (to? And ampersand. In development, the incoming parameter that sends the request is mostly an object. When sending, if the request is a GET request, the parameters need to be converted. Using this library, you can convert automatically without having to manually concatenate. More reference
  • The default content-type for Axios is Application/JSON. If qs is used for serialization then the content-type is Application/X-www-form-urlencoded or form submission.