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

首頁 > 編程 > JavaScript > 正文

Vue 中axios配置實例詳解

2019-11-19 13:24:12
字體:
供稿:網(wǎng)友

1.GET 請求

//向具有指定ID的用戶發(fā)出請求axios.get('/user?ID=12345').then(function (response) {console.log(response);}).catch(function (error) {console.log(error);});// 也可以通過 params 對象傳遞參數(shù)axios.get('/user', {params: {ID: 12345}}).then(function (response) {console.log(response);}).catch(function (error) {console.log(error);});

2.POST請求

axios.post('/user', {firstName: 'Fred',lastName: 'Flintstone'}).then(function (response) {console.log(response);}).catch(function (error) {console.log(error);});

3執(zhí)行多個并發(fā)請求

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) {//兩個請求現(xiàn)已完成}));

4.請求配置

這些是用于發(fā)出請求的可用配置選項。 只有url是必需的。 如果未指定方法,請求將默認為GET.

{  // `url`是將用于請求的服務(wù)器URL  url: '/user',  // `method`是發(fā)出請求時使用的請求方法  method: 'get', // 默認  // `baseURL`將被添加到`url`前面,除非`url`是絕對的。  // 可以方便地為 axios 的實例設(shè)置`baseURL`,以便將相對 URL 傳遞給該實例的方法。  baseURL: 'https://some-domain.com/api/',  // `transformRequest`允許在請求數(shù)據(jù)發(fā)送到服務(wù)器之前對其進行更改  // 這只適用于請求方法'PUT','POST'和'PATCH'  // 數(shù)組中的最后一個函數(shù)必須返回一個字符串,一個 ArrayBuffer或一個 Stream  transformRequest: [function (data) {  // 做任何你想要的數(shù)據(jù)轉(zhuǎn)換  return data;  }],  // `transformResponse`允許在 then / catch之前對響應(yīng)數(shù)據(jù)進行更改  transformResponse: [function (data) {  // Do whatever you want to transform the data  return data;  }],  // `headers`是要發(fā)送的自定義 headers  headers: {'X-Requested-With': 'XMLHttpRequest'},  // `params`是要與請求一起發(fā)送的URL參數(shù)  // 必須是純對象或URLSearchParams對象  params: {  ID: 12345  },  // `paramsSerializer`是一個可選的函數(shù),負責(zé)序列化`params`  // (e.g. https://www.npmjs.com/package/qs, http://api.jquery.com/jquery.param/)  paramsSerializer: function(params) {  return Qs.stringify(params, {arrayFormat: 'brackets'})  },  // `data`是要作為請求主體發(fā)送的數(shù)據(jù)  // 僅適用于請求方法“PUT”,“POST”和“PATCH”  // 當(dāng)沒有設(shè)置`transformRequest`時,必須是以下類型之一:  // - string, plain object, ArrayBuffer, ArrayBufferView, URLSearchParams  // - Browser only: FormData, File, Blob  // - Node only: Stream  data: {  firstName: 'Fred'  },  // `timeout`指定請求超時之前的毫秒數(shù)。  // 如果請求的時間超過'timeout',請求將被中止。  timeout: 1000,  // `withCredentials`指示是否跨站點訪問控制請求  // should be made using credentials  withCredentials: false, // default  // `adapter'允許自定義處理請求,這使得測試更容易。  // 返回一個promise并提供一個有效的響應(yīng)(參見[response docs](#response-api))  adapter: function (config) {  /* ... */  },  // `auth'表示應(yīng)該使用 HTTP 基本認證,并提供憑據(jù)。  // 這將設(shè)置一個`Authorization'頭,覆蓋任何現(xiàn)有的`Authorization'自定義頭,使用`headers`設(shè)置。  auth: {  username: 'janedoe',  password: 's00pers3cret'  },  // “responseType”表示服務(wù)器將響應(yīng)的數(shù)據(jù)類型  // 包括 'arraybuffer', 'blob', 'document', 'json', 'text', 'stream'  responseType: 'json', // default  //`xsrfCookieName`是要用作 xsrf 令牌的值的cookie的名稱  xsrfCookieName: 'XSRF-TOKEN', // default  // `xsrfHeaderName`是攜帶xsrf令牌值的http頭的名稱  xsrfHeaderName: 'X-XSRF-TOKEN', // default  // `onUploadProgress`允許處理上傳的進度事件  onUploadProgress: function (progressEvent) {  // 使用本地 progress 事件做任何你想要做的  },  // `onDownloadProgress`允許處理下載的進度事件  onDownloadProgress: function (progressEvent) {  // Do whatever you want with the native progress event  },  // `maxContentLength`定義允許的http響應(yīng)內(nèi)容的最大大小  maxContentLength: 2000,  // `validateStatus`定義是否解析或拒絕給定的promise  // HTTP響應(yīng)狀態(tài)碼。如果`validateStatus`返回`true`(或被設(shè)置為`null` promise將被解析;否則,promise將被   // 拒絕。  validateStatus: function (status) {  return status >= 200 && status < 300; // default  },  // `maxRedirects`定義在node.js中要遵循的重定向的最大數(shù)量。  // 如果設(shè)置為0,則不會遵循重定向。  maxRedirects: 5, // 默認  // `httpAgent`和`httpsAgent`用于定義在node.js中分別執(zhí)行http和https請求時使用的自定義代理。  // 允許配置類似`keepAlive`的選項,  // 默認情況下不啟用。  httpAgent: new http.Agent({ keepAlive: true }),  httpsAgent: new https.Agent({ keepAlive: true }),  // 'proxy'定義代理服務(wù)器的主機名和端口  // `auth`表示HTTP Basic auth應(yīng)該用于連接到代理,并提供credentials。  // 這將設(shè)置一個`Proxy-Authorization` header,覆蓋任何使用`headers`設(shè)置的現(xiàn)有的`Proxy-Authorization` 自定義 headers。  proxy: {  host: '127.0.0.1',  port: 9000,  auth: : {  username: 'mikeymike',  password: 'rapunz3l'  }  },  // “cancelToken”指定可用于取消請求的取消令牌  // (see Cancellation section below for details)  cancelToken: new CancelToken(function (cancel) {  }) }

5.全局axios默認值

 axios.defaults.baseURL = 'https://api.example.com'; axios.defaults.headers.common['Authorization'] = AUTH_TOKEN; axios.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded';

6.攔截器

你可以截取請求或響應(yīng)在被 then 或者 catch 處理之前

//添加請求攔截器<==>請求發(fā)起前做的事axios.interceptors.request.use(function(config){   //在發(fā)送請求之前做某事   return config;  },function(error){   //請求錯誤時做些事   return Promise.reject(error);  });//添加響應(yīng)攔截器<==>響應(yīng)回來后做的事axios.interceptors.response.use(function(response){   //對響應(yīng)數(shù)據(jù)做些事   return response;  },function(error){   //請求錯誤時做些事   return Promise.reject(error);  });

   如果你以后可能需要刪除攔截器。、

 var myInterceptor = axios.interceptors.request.use(function () {/*...*/});  axios.interceptors.request.eject(myInterceptor);

    你可以將攔截器添加到axios的自定義實例

  var instance = axios.create();  instance.interceptors.request.use(function () {/*...*/});

總結(jié)

以上所述是小編給大家介紹的Vue 中axios配置實例詳解,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復(fù)大家的。在此也非常感謝大家對武林網(wǎng)網(wǎng)站的支持!

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 永福县| 兴安县| 饶阳县| 攀枝花市| 孟村| 乳山市| 久治县| 常山县| 怀宁县| 临海市| 互助| 辽宁省| 伊通| 临汾市| 陕西省| 米脂县| 东宁县| 恩施市| 呼图壁县| 崇义县| 会同县| 台东县| 桂平市| 峨眉山市| 武穴市| 安阳县| 蚌埠市| 宁夏| 泸定县| 甘德县| 胶州市| 靖宇县| 额尔古纳市| 博野县| 八宿县| 卢氏县| 莱州市| 扶余县| 犍为县| 哈巴河县| 犍为县|