vue-axios
GET請求
axios.get("/user?id=1") .then(function(response){ }) .catch(function(error){ })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 }));配置
import Qs from 'qs'{ //請求的接口,在請求的時候,如axios.get(url,config);這里的url會覆蓋掉config中的url url: '/user', // 請求方法同上 method: 'get', // default // 基礎url前綴 baseURL: 'https://some-domain.com/api/', transformRequest: [function (data) { // 這里可以在發送請求之前對請求數據做處理,比如form-data格式化等,這里可以使用開頭引入的Qs(這個模塊在安裝axios的時候就已經安裝了,不需要另外安裝) data = Qs.stringify({}); return data; }], transformResponse: [function (data) { // 這里提前處理返回的數據 return data; }], // 請求頭信息 headers: {'X-Requested-With': 'XMLHttpRequest'}, //parameter參數 params: { ID: 12345 }, //post參數,使用axios.post(url,{},config);如果沒有額外的也必須要用一個空對象,否則會報錯 data: { firstName: 'Fred' }, auth: { username: 'janedoe', password: 's00pers3cret' }, //設置超時時間 timeout: 1000, //返回數據類型 responseType: 'json', // default .....等等}以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持武林網。
新聞熱點
疑難解答