国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > JavaScript > 正文

基于Axios 常用的請求方法別名(詳解)

2019-11-19 14:10:48
字體:
來源:轉載
供稿:網友

Axios

是一個基于 promise 的 HTTP 庫,可以用在瀏覽器和 node.js 中。

常用的請求方法別名一般有: Get/post/http協議請求

執行Get請求

function get(){ return axios.get('/data.json', {    params:{     id:1234    }    }).then(function (response) {     console.log(response);    })   .catch(function (error) {    console.log(error);   }); }

使用get方法進行傳參數的時候用的是 params方法

執行Post請求

function post(){return axios.post('/data.json', {  id:1234    })  .then(function (response) {    console.log(response);  })  .catch(function (error) {    console.log(error);  }); }

使用post方法進行傳參數的時候是直接進行數據的傳遞,這也是兩種方法的區別。

執行http協議請求

function http(){ return axios({ method: 'post', url: '/data.json', data: {  id: 1111, },params: { id:2222, }).then(res=>{  this.msg=res.data; });}

注意這里的區別,當使用post請求的時候,進行數據的傳參使用的是data方法,而使用get請求的時候,使用的是params方法。

使用攔截器:

在請求或響應被 then 或 catch 處理前攔截它們。

// 添加請求攔截器mounted:function(){  axios.interceptors.request.use(function (config) {    // 在發送請求之前做些什么    return config;   }, function (error) {    // 對請求錯誤做些什么    return Promise.reject(error);   });// 添加響應攔截器  axios.interceptors.response.use(function (response) {    // 對響應數據做點什么    return response;   }, function (error) {    // 對響應錯誤做點什么    return Promise.reject(error);   });}

以上這篇基于Axios 常用的請求方法別名(詳解)就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持武林網。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 临沭县| 宁河县| 宜良县| 加查县| 长武县| 高平市| 静宁县| 临湘市| 远安县| 南乐县| 霍邱县| 克山县| 南溪县| 全椒县| 桐柏县| 北票市| 岳阳县| 晋江市| 平度市| 乐陵市| 珲春市| 封开县| 惠东县| 松原市| 景谷| 揭东县| 定州市| 南宫市| 綦江县| 绍兴市| 腾冲县| 望都县| 肃宁县| 合肥市| 梅州市| 天门市| 青田县| 龙陵县| 诏安县| 广元市| 沾化县|