axios全局配置

介绍

  • axios.defaults.timeout=30000;:超时时间,单位毫秒
  • axios.defaults.baseURL = 'https://api.example.com'; :默认地址,发请求时,可以直接写后面的路径
  • axios.defaults.withCredentials = true;:跨域携带cookie,详见 链接
  • axios.defaults.headers['Auth'] = 'AUTH_TOKEN';
  • axios.defaults.headers.common['Authorization'] = 'AUTH_TOKEN';
  • axios.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded';

例子

在 axios get请求 基础上修改,如下:

<script src="https://unpkg.com/axios/dist/axios.min.js"></script>
<script>
axios.defaults.withCredentials = true;//带cookie
axios.defaults.timeout=30000;
axios.defaults.baseURL = 'http://localhost';

axios.get('/hello.json').then(function(res){
    console.log(res);
    console.log(res.data);
})
</script>

原文出处:/show_1IXIantsrcJ.html