axios.get('/user?ID=12345') .then(function (response) { console.log(response); console.log(response.data); console.log(response.status); console.log(response.statusText); console.log(response.headers); console.log(response.config); }) .catch(function (error) { console.log(error); });
// 發送 POST 請求 axios({ method: 'post', url: '/user/12345', data: { firstName: 'Fred', lastName: 'Flintstone' } }); axios.post('/user', { firstName: 'Fred', lastName: 'Flintstone' }) .then(function (response) { console.log(response); }) .catch(function (error) { console.log(error); });
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) { // 兩個請求如今都執行完成 }));
封裝請求:ios
function ajax_post(url,data,that,callback){ axios({ method:"POST", headers:{'Content-type':'application/json',}, url:URL+url, data:data, //withCredentials:true }).then(function(res){ //alert('post:'+res) console.log(url+'\tPost請求到:'); console.log(res); //alert('post-response:'+res); callback(that,res); //ajax_get('/manage/getinfo',this); }).catch(function(error){ alert('post失敗') console.log(error); }); } function ajax_get(url,that,callback){ axios({ method:"GET", headers:{'Content-type':'application/json',}, url:URL+url, withCredentials:true }).then(function(res){ console.log(url+'\tGet請求到:') console.log(res); //alert('get:'+this.res); callback(that,res); }).catch(function(error){ alert('get下載失敗') console.log(error); }); } function ajax_post_params(url,data,that,callback=()=>{}){ axios({ method: 'post', url: URL+url, headers: { 'Content-type': 'application/x-www-form-urlencoded', }, params:data, }) .then(function(res){ //alert('post:'+res) console.log(url+'\tPost請求到:'); console.log(res); //alert('post-response:'+res); callback(that,res); //ajax_get('/manage/getinfo',this); }).catch(function(error){ alert('post失敗') console.log(error); }); }