Axios

2020-04-20  本文已影响0人  游_弋

axios({

    method:'post',

    url: '/user/123',

    data:{

        msg: '123'

    }

})

axios.get("url",{    params:{ },  headers:{ token:"jack"}    }).then(res=>{  }).catch(function (error) {  });

axios.post("url",{},{  headers:{  token:"tom"  }  }).then(res=>{  }).catch(err=>{ })

axios.delete('url?id='+id).then()

axios全局配置

axios.defaults.timeout = 1000;  //请求超时时间

axios.defaults.baseURL =  'localhost:8080';  //默认地址前戳 

axios.defaults.headers.post['Content-Type'] = 'application/json'   //POST参数格式

axios实例配置

let axios1 = axios.create();

// 创建axios实例 // 请求超时时间var instance = axios.create({    timeout: 1000 * 12,    withCredentials: true,    baseURL: '/api',    })

axios1.default.timeout = 3000;

axios请求配置

axios1.get( url,{ timeout: 5000 } )

拦截器: 在请求或者响应被处理前拦截它们----请求拦截器  <==>  响应拦截器

请求拦截器  

axios.interceptors.request.use( config => { /*发送前做些什么*/ config.hearders['token'] = ''  return config},err => { /*请求错误做些什么*/ return Promise.reject(err) } )

取消拦截器  axios.interceptors.request.eject(interceptors)

axios.all( [   //并发请求

    axios.get(url),

    axios.get(url)

] ).then( 

    axios.spread( (get1,get2)=>{   //对应返回参数

     } )

)

file文件上传

<input type="file" @change="getFile" /> 

getFile(e){ e.target.files[0] }

var fd = new FormData();

fd.append('file',e.target.files[0])

this.$axios.post('upload',fd,{

cancelToken: source.token, // 携带取消标识

  onUploadProgress: (progressEvent) => {

      this.fileLoad = Math.ceil( (progressEvent.loaded / progressEvent.total)*100 ); //上传进度

  }

})

var source = axios.CancelToken.source();

source.cancel( '请求被取消了' )

上一篇 下一篇

猜你喜欢

热点阅读