promise----->new Promise((resolve,rejust)=>{ resolve('我是成功的') }).then(res=>{ }) PromiseAll----->处理并发(同时进行) new PromiseAll([ new Promise(), new Promise() ]).then(res=>{})
通过npm安装axios: npm i axios -S---->(-S代表生产环境)
get请求方式: axios.get(url,date);//可以在url后面拼接参数
post请求方式: axios.post('url',date);
axios并发请求: axios.all([ axios.post('url',date), axios.post('url',date), axios.get(url,date) ]).then(res=>{}).catch(err=>{})//请求成功后返回以上并发请求返回的数据的数组(合并了)
axios全局配置: //配置全局url axios.default.baseURL="http://127.0.0.1"; //配置超时时间 axios.default.timeout=5000; //配置头信息 axios.default.headers.post['content-type']='application/x-www-form-urlencoded'; //配置串行化json axios.default.transformRequest=function(date){ date=JSON.stringify(date); return data; }
axios配置实例:--->可以配置多个域名(不同的url) let local=axios.create({ baseURL:"http://127.0.0.1", timeout=5000 })
使用:--->local.get('/getId')
let local1=axios.create({ baseURL:"http://192.168.1.3", timeout=5000 })
使用:--->local1.post('/getId',date);
axios请求拦截器的使用:
①请求拦截器:判断token(前端--->后端) axiox.interceptors.request.use( //拦截成功后处理,然后放行 config=>{
return config;//放行所有请求 }, //拦截失败处理 err=>{}
);
②响应拦截器:判断是否请求成功(后端--->前端) axiox.interceptors.response.use( //拦截成功后处理,然后放行 config=>{
return config;//放行所有请求 }, //拦截失败处理 err=>{}
);