發起一個GET
請求
// get傳參數 axios.get('/user?ID=12345') .then(function (response) { console.log(response); }) .catch(function (error) { console.log(error); }); // get傳參數 后端接收 req.query axios.get('/user', { params: { ID: 12345 }
}) .then(function (response) { console.log(response); }) .catch(function (error) { console.log(error); });
發起一個POST
請求
//post傳參數
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) { // Both requests are now complete }));
axios 通用寫法
// 發送 POST 請求 axios({ method: 'post', url: '/user/12345', data: { firstName: 'Fred', lastName: 'Flintstone' } }); // GET 請求遠程圖片 axios({ method:'get', url:'http://bit.ly/2mTM3nY', responseType:'stream' })
請求方法別名
為了方便我們為所有支持的請求方法均提供了別名。
axios.request(config)
axios.get(url[, config])
axios.delete(url[, config])
axios.head(url[, config])
axios.options(url[, config])
axios.post(url[, data[, config]])
axios.put(url[, data[, config]])
axios.patch(url[, data[, config]])
參考鏈接:https://www.jianshu.com/p/7a9fbcbb1114