做爰高潮a片〈毛片〉,尤物av天堂一区二区在线观看,一本久久A久久精品VR综合,添女人荫蒂全部过程av

最新文章專題視頻專題問答1問答10問答100問答1000問答2000關鍵字專題1關鍵字專題50關鍵字專題500關鍵字專題1500TAG最新視頻文章推薦1 推薦3 推薦5 推薦7 推薦9 推薦11 推薦13 推薦15 推薦17 推薦19 推薦21 推薦23 推薦25 推薦27 推薦29 推薦31 推薦33 推薦35 推薦37視頻文章20視頻文章30視頻文章40視頻文章50視頻文章60 視頻文章70視頻文章80視頻文章90視頻文章100視頻文章120視頻文章140 視頻2關鍵字專題關鍵字專題tag2tag3文章專題文章專題2文章索引1文章索引2文章索引3文章索引4文章索引5123456789101112131415文章專題3
問答文章1 問答文章501 問答文章1001 問答文章1501 問答文章2001 問答文章2501 問答文章3001 問答文章3501 問答文章4001 問答文章4501 問答文章5001 問答文章5501 問答文章6001 問答文章6501 問答文章7001 問答文章7501 問答文章8001 問答文章8501 問答文章9001 問答文章9501
當前位置: 首頁 - 科技 - 知識百科 - 正文

axios怎樣基于Promise的HTTP請求客戶端

來源:懂視網 責編:小采 時間:2020-11-27 18:49:13
文檔

axios怎樣基于Promise的HTTP請求客戶端

axios怎樣基于Promise的HTTP請求客戶端:這次給大家帶來axios怎樣基于Promise的HTTP請求客戶端,axios基于Promise的HTTP請求客戶端的注意事項有哪些,下面就是實戰案例,一起來看一下。axios基于 Promise 的 HTTP 請求客戶端,可同時在瀏覽器和 node.js 中使用功能特性在瀏覽器中發送XML
推薦度:
導讀axios怎樣基于Promise的HTTP請求客戶端:這次給大家帶來axios怎樣基于Promise的HTTP請求客戶端,axios基于Promise的HTTP請求客戶端的注意事項有哪些,下面就是實戰案例,一起來看一下。axios基于 Promise 的 HTTP 請求客戶端,可同時在瀏覽器和 node.js 中使用功能特性在瀏覽器中發送XML

這次給大家帶來axios怎樣基于Promise的HTTP請求客戶端,axios基于Promise的HTTP請求客戶端的注意事項有哪些,下面就是實戰案例,一起來看一下。

axios

基于 Promise 的 HTTP 請求客戶端,可同時在瀏覽器和 node.js 中使用

功能特性

在瀏覽器中發送XMLHttpRequests請求

在 node.js 中發送http請求

支持PromiseAPI

攔截請求和響應

轉換請求和響應數據

自動轉換 JSON 數據

客戶端支持保護安全免受XSRF攻擊

瀏覽器支持

安裝

使用 bower:

$ bower install axios

使用 npm:

$ npm install axios

例子

發送一個GET請求

// Make a request for a user with a given IDaxios.get('/user?ID=12345').then(function(response){console.log(response);}).catch(function(response){console.log(response);});
// Optionally the request above could also be done asaxios.get('/user',{params:{ID:12345}}).then(function(response){console.log(response);}).catch(function(response){console.log(response);});

發送一個POST請求

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

發送多個并發請求

functiongetUserAccount(){returnaxios.get('/user/12345');}functiongetUserPermissions(){returnaxios.get('/user/12345/permissions');}axios.all([getUserAccount(),getUserPermissions()]).then(axios.spread(function(acct,perms){// Both requests are now complete}));

axios API

可以通過給axios傳遞對應的參數來定制請求:

axios(config)
// Send a POST requestaxios({method:'post',url:'/user/12345',data:{firstName:'Fred',lastName:'Flintstone'}});
axios(url[, config])
// Sned a GET request (default method)axios('/user/12345');

請求方法別名

為方便起見,我們為所有支持的請求方法都提供了別名

axios.get(url[, config])
axios.delete(url[, config])
axios.head(url[, config])
axios.post(url[, data[, config]])
axios.put(url[, data[, config]])
axios.patch(url[, data[, config]])

注意

當使用別名方法時,url、method和data屬性不需要在 config 參數里面指定。

并發

處理并發請求的幫助方法

axios.all(iterable)
axios.spread(callback)

創建一個實例

你可以用自定義配置創建一個新的 axios 實例。

axios.create([config])
varinstance=axios.create({baseURL:'https://some-domain.com/api/',timeout:1000,headers:{'X-Custom-Header':'foobar'}});

實例方法

所有可用的實例方法都列在下面了,指定的配置將會和該實例的配置合并。

axios#request(config)
axios#get(url[, config])
axios#delete(url[, config])
axios#head(url[, config])
axios#post(url[, data[, config]])
axios#put(url[, data[, config]])
axios#patch(url[, data[, config]])

請求配置

下面是可用的請求配置項,只有url是必需的。如果沒有指定method,默認的請求方法是GET。

{// `url` is the server URL that will be used for the requesturl:'/user',
// `method` is the request method to be used when making the requestmethod:'get',
// default// `baseURL` will be prepended to `url` unless `url` is absolute.
// It can be convenient to set `baseURL` for an instance of axios to pass relative URLs
// to methods of that instance.baseURL:' 
// `transformRequest` allows changes to the request data before it is sent to the server
// This is only applicable for request methods 'PUT', 'POST', and 'PATCH'
// The last function in the array must return a string or an ArrayBuffertransformRequest:[function(data){
// Do whatever you want to transform the datareturndata;}],
// `transformResponse` allows changes to the response data to be made before
// it is passed to then/catchtransformResponse:[function(data){
// Do whatever you want to transform the datareturndata;}],
// `headers` are custom headers to be sentheaders:{'X-Requested-With':'XMLHttpRequest'},
// `params` are the URL parameters to be sent with the requestparams:{ID:12345},
// `paramsSerializer` is an optional function in charge of serializing `params`
// (e.g. https://www.npmjs.com/package/qs, paramsSerializer:function(params){returnQs.stringify(params,{arrayFormat:'brackets'})},
// `data` is the data to be sent as the request body// Only applicable for request methods 'PUT', 'POST', and 'PATCH'
// When no `transformRequest` is set, must be a string, an ArrayBuffer or a hashdata:{firstName:'Fred'},
// `timeout` specifies the number of milliseconds before the request times out.
// If the request takes longer than `timeout`, the request will be aborted.timeout:1000,
// `withCredentials` indicates whether or not cross-site Access-Control requests
// should be made using credentialswithCredentials:false,
// default// `adapter` allows custom handling of requests which makes testing easier.
// Call `resolve` or `reject` and supply a valid response (see [response docs](#response-api)).adapter:function(resolve,reject,config){/* ... */},
// `auth` indicates that HTTP Basic auth should be used, and supplies credentials.
// This will set an `Authorization` header, overwriting any existing
// `Authorization` custom headers you have set using `headers`.auth:{username:'janedoe',password:'s00pers3cret'}
// `responseType` indicates the type of data that the server will respond with
// options are 'arraybuffer', 'blob', 'document', 'json', 'text'responseType:'json',
// default// `xsrfCookieName` is the name of the cookie to use as a value for xsrf tokenxsrfCookieName:'XSRF-TOKEN',
// default// `xsrfHeaderName` is the name of the http header that carries the xsrf token valuexsrfHeaderName:'X-XSRF-TOKEN',
// default// `progress` allows handling of progress events for 'POST' and 'PUT uploads'
// as well as 'GET' downloadsprogress:function(progressEvent){
// Do whatever you want with the native progress event}}

響應的數據結構

響應的數據包括下面的信息:

{// `data` is the response that was provided by the serverdata:{},
// `status` is the HTTP status code from the server responsestatus:200,
// `statusText` is the HTTP status message from the server responsestatusText:'OK',
// `headers` the headers that the server responded withheaders:{},
// `config` is the config that was provided to `axios` for the requestconfig:{}}

當使用then或者catch時, 你會收到下面的響應:

axios.get('/user/12345').then(function(response){console.log(response.data);console.log(response.status);console.log(response.statusText);console.log(response.headers);console.log(response.config);});

默認配置

你可以為每一個請求指定默認配置。

全局 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';

自定義實例默認配置

// Set config defaults when creating the instancevarinstance=axios.create({baseURL:' 
// Alter defaults after instance has been createdinstance.defaults.headers.common['Authorization']=AUTH_TOKEN;

配置的優先順序

Config will be merged with an order of precedence. The order is library defaults found inlib
/defaults.js, thendefaultsproperty of the instance, and finallyconfigargument for the request. The latter will take precedence over the former. Here's an example.
// Create an instance using the config defaults provided by the library
// At this point the timeout config value is `0` as is the default for the libraryvarinstance=axios.create();
// Override timeout default for the library
// Now all requests will wait 2.5 seconds before timing outinstance.defaults.timeout=2500;
// Override timeout for this request as it's known to take a long timeinstance.get('/longRequest',{timeout:5000});

你可以在處理then或catch之前攔截請求和響應

// 添加一個請求axios.interceptors.request.use(function(config){
// Do something before request is sentreturnconfig;},function(error){
// Do something with request errorreturnPromise.reject(error);});
// 添加一個響應axios.interceptors.response.use(function(response){
// Do something with response datareturnresponse;},function(error){
// Do something with response errorreturnPromise.reject(error);});

移除一個:

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

你可以給一個自定義的 axios 實例添加:

varinstance=axios.create();instance.interceptors.request.use(function(){/*...*/});
錯誤處理
axios.get('/user/12345').catch(function(response){if(responseinstanceofError){
// Something happened in setting up the request that triggered an Errorconsole.log('Error',response.message);}else{
// The request was made, but the server responded with a status code
// that falls out of the range of 2xxconsole.log(response.data);console.log(response.status);console.log(response.headers);console.log(response.config);}});
Promises
axios 依賴一個原生的 ES6 Promise 實現,如果你的瀏覽器環境不支持 ES6 Promises,你需要引入polyfill
TypeScript
axios 包含一個TypeScript定義
/// import*asaxiosfrom'axios';axios.get('/user?ID=12345');
Credits
axios is heavily inspired by the$http serviceprovided inAngular. Ultimately axios is an effort to provide a standalone$http-like service for use outside of Angular.
License
MIT

相信看了本文案例你已經掌握了方法,更多精彩請關注Gxl網其它相關文章!

相關閱讀:

VUE如何使用anmate.css

css的漸變顏色

好用的404組件

解決Iview在vue-cli架子中字體圖標丟失的方法

聲明:本網頁內容旨在傳播知識,若有侵權等問題請及時與本網聯系,我們將在第一時間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com

文檔

axios怎樣基于Promise的HTTP請求客戶端

axios怎樣基于Promise的HTTP請求客戶端:這次給大家帶來axios怎樣基于Promise的HTTP請求客戶端,axios基于Promise的HTTP請求客戶端的注意事項有哪些,下面就是實戰案例,一起來看一下。axios基于 Promise 的 HTTP 請求客戶端,可同時在瀏覽器和 node.js 中使用功能特性在瀏覽器中發送XML
推薦度:
標簽: 請求 客戶端 http
  • 熱門焦點

最新推薦

猜你喜歡

熱門推薦

專題
Top
主站蜘蛛池模板: 大名县| 莱阳市| 建湖县| 婺源县| 威信县| 景东| 资源县| 浏阳市| 武功县| 韶关市| 龙岩市| 临高县| 红河县| 灵武市| 鄂伦春自治旗| 永州市| 开江县| 浪卡子县| 元江| 日喀则市| 蒲江县| 乐安县| 旌德县| 婺源县| 西丰县| 大连市| 墨玉县| 伊宁县| 威信县| 吉安县| 泉州市| 乌拉特中旗| 洛阳市| 建阳市| 吉木乃县| 个旧市| 临泉县| 阿鲁科尔沁旗| 化隆| 江安县| 宣城市|