$npm install axios
Get请求
//通过给定的ID来发送请求 axios.get(‘/user?ID=12345‘) .then((response)=>{ console.log(response); }) .catch((err)=>{ console.log(err); }); //以上请求也可以通过这种方式来发送 axios.get(‘/user‘, { params:{ ID:12345 } }) .then((response)=>{ console.log(response); }) .catch((err)=>{ console.log(err); });
post请求
axios.post(‘/user‘, { firstName:‘Fred‘, lastName:‘Flintstone‘ }) .then(function(res){ console.log(res); }) .catch(function(err){ console.log(err); });
一次性发送多个
function getUserAccount(){ return axios.get(‘/user/12345‘); } function getUserPermissions(){ return axios.get(‘/user/12345/permissions‘); } axios.all([getUserAccount(),getUserPermissions()]) .then(axios.spread(function(acct,perms){ //当这两个请求都完成的时候会触发这个函数,两个参数分别代表返回的结果 }))
-------------------整理于yingxiang 20190218
原文:https://www.cnblogs.com/522040-m/p/10396967.html