axios

axios基本使用

  axios({

    method: 'get',

    url: 'xxx',

    params: data,

    headers: {token: 'xxxxx'}

  }).then(res => {console.log(res)})

  axios({

    method: 'post',

    url: 'xxx',

    data: data,

    headers: {token: 'xxxx'}

  }).then(res => {console.log(res)}) 

  axios.get('xxx', {params: data}).then(res => {console.log(res)})

  axios.post('xxx', data).then(res => {console.log(res)})

封装axios,request.js

  import  axios  from  'axios';

  axios.defaults.baseURL  =  'http://192.168.1.130:8020'

  // 请求拦截器

  axios.interceptors.request.use(function(confit){

    // 在发送请求前做相应的操作

    config.headers["token"] = ''

    return  config;

  }, function(error){

    //请求出错

    return  Promise.reject(error);

  })

  // 响应拦截器

  axios.interceptors.response.use(function(res){

    // 返回响应数据前做相应操作

    return  res;

  }, function(error){

    return  Promise.reject(error);

  })

  

  export  default  axios;

原文地址:https://www.cnblogs.com/cuishuangshuang/p/13473458.html