axios中文說(shuō)明Axios是一個(gè)基于promise的HTTP庫(kù)可以用在瀏覽器_第1頁(yè)
axios中文說(shuō)明Axios是一個(gè)基于promise的HTTP庫(kù)可以用在瀏覽器_第2頁(yè)
axios中文說(shuō)明Axios是一個(gè)基于promise的HTTP庫(kù)可以用在瀏覽器_第3頁(yè)
axios中文說(shuō)明Axios是一個(gè)基于promise的HTTP庫(kù)可以用在瀏覽器_第4頁(yè)
axios中文說(shuō)明Axios是一個(gè)基于promise的HTTP庫(kù)可以用在瀏覽器_第5頁(yè)
已閱讀5頁(yè),還剩10頁(yè)未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說(shuō)明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡(jiǎn)介

AxiospromiseHTTPnode.jsnode.jshttp支持PromiseAPI自動(dòng)轉(zhuǎn)換JSON數(shù)據(jù)LatestLatestLatestLatestLatestLatest8+$$npminstall使用$$bowerinstall<script<script執(zhí) 請(qǐng)為給定ID的user.then(function{.catch(function{{params:{ID:}.then(function{.catch(function{執(zhí) 請(qǐng){firstName:'Fred',lastName:'Flintstone'.then(function{.catch(function{functionfunction{return}functiongetUserPermissions()return}.then(axios.spread(function(acct,perms)axios 發(fā)送發(fā)送POST請(qǐng)求method:'post',data:{firstName:'Fred',lastName:'Flintstone'}axios(url[,發(fā)送GET請(qǐng)求(默認(rèn)的方法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]]) 并創(chuàng)建實(shí)

varvarinstance=axios.create({baseURL:'httpstimeout:headers:{'X-Custom-Header':實(shí)例方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`是用于請(qǐng)求的服務(wù)器URLurl:'/user',`method`methodget'默認(rèn)是//`baseURL`將自動(dòng)加在`url`前面,除非`url`是一個(gè)絕對(duì)URL//它可以通過(guò)設(shè)置一個(gè)`baseURL`便于為axios實(shí)例的方法傳遞相對(duì)URLbaseURL:'https 只能用在'PUT'POST和'PATCH//后面數(shù)組中的函數(shù)必須返回一個(gè)字符串,或ArrayBuffer,或StreamtransformRequest:[function(data){對(duì)datareturn`transformResponse`在傳遞給then/catchtransformResponse:[function(data)對(duì)datareturn`headers`headers:{'X-Requested-With'://`params`是即將與請(qǐng)求一起發(fā)送的URL//必須是一個(gè)無(wú)格式對(duì)象 inobject)或URLSearchParams對(duì){ID:`paramsSerializer`是一個(gè)負(fù)責(zé)`params`//(e.g. /package/qs, paramsSerializer:function(params)returnQs.stringify(params,{arrayFormat:`data`//只適用于這些請(qǐng)求方法'PUT'POST',和在沒有設(shè)置`transformRequest`//- inobject,ArrayBuffer,ArrayBufferView,//-瀏覽器專 FormData,File,//-Node專屬 data:{firstName:`timeout`指定請(qǐng)求超時(shí)的毫秒數(shù)(0表示無(wú)超時(shí)時(shí)間//如果請(qǐng)求話費(fèi)了超過(guò)`timeout`timeout:withCredentials:false`adapter`返回一個(gè)promise并應(yīng)用一個(gè)有效的響應(yīng)(查閱[responsedocs](#response-api)).adapter:function(config){/*...//`auth`表示應(yīng)該使用HTTP//這將設(shè)置一個(gè)`Authorization`頭,覆寫掉現(xiàn)有的任意使用`headers`設(shè)置的自定義`Authorizatioauth:username:'janedoe',password:'s00pers3cret'//`responseType`表示服務(wù)器響應(yīng)的數(shù)據(jù)類型,可以是'arraybuffer','blob',' ','json','text','stream'responseType:'json// Name`是用作xsrftoken的值 Name:'XSRF-TOKEN',//`xsrfHeaderName`是承載xsrftoken的值的HTTPxsrfHeaderName:'X-XSRF-TOKEN'`onUploadProgress`onUploadProgress:function(progressEvent)//對(duì)原生進(jìn) //`onDownloadProgress`允許 onDownloadProgress:function(progressEvent)//對(duì)原生進(jìn) //`validateStatus`定義對(duì)于給定的HTTP響應(yīng)狀態(tài)碼是resolve或rejectpromise。如果`validateStatus`返回`true`(或者設(shè)置為`null`或`undefined`),promise將被resolve;否則,promise將被rejectevalidateStatus:function(status)returnstatus200&&status300`maxRedirects`定義在node.js中follow如果設(shè)置為0,將不會(huì)followmaxRedirects:5,`httpAgent`和`httpsAgent`分別在node.js中用于定義在執(zhí)行http和https`keepAlive`httpAgent:newhttp.Agent({keepAlive:truehttpsAgent:newhttps.Agent({keepAlive:true// '定 //`auth`表示HTTP基礎(chǔ)驗(yàn)證應(yīng)當(dāng)用于連 //這將會(huì)設(shè)置一個(gè) -Authorization`頭,覆寫掉已有的通過(guò)使用`header`設(shè)置的自定義:host:auth::{username:'mikeymike',password:'rapunz3l'}`cancelToken`指定用于取消請(qǐng)求的cancel//(查看后面的Cancellation這節(jié)了 cancelToken:newCancelToken(function(cancel)}{{`data`data://`status`來(lái)自服務(wù)器響應(yīng)的HTTPstatus:`statusText`來(lái)自服務(wù)器響應(yīng)的HTTPstatusText:`headers`headers:`config`config:}使 {console.log(response.data); 時(shí),或傳遞rejectioncallback作為 配置的默認(rèn)值axios //此時(shí)超時(shí)配置的默認(rèn)值是varinstance=//現(xiàn)在,在超時(shí)前,所有請(qǐng)求都會(huì)等待2.5instance.defaults.timeout={timeout:器 ////添加請(qǐng) return},function(error)return//添加響 return},function(error)returnvarvarmyInterceptor=erceptors.request.use(function(){/*...*/});可以為自定義axios實(shí)例添加varinstance=axios.create();erceptors.request.use(functionvarinstance=axios.create();erceptors.request.use(function(){/*...*/}); 配置選項(xiàng)定義一個(gè)自定義HTTP狀態(tài)碼的錯(cuò)誤范圍{validateStatus:function(status)returnstatus500狀態(tài)碼在大于或等于500時(shí)才會(huì)}canceltokenvarCancelToken=axios.CancelToken;varsource=CancelToken.source();{cancelToken:}).catch(function(thrown){if(axios.isCancel(thrown)){console.log('Requestcanceled',}else}取消請(qǐng)求(message參數(shù)是可選的source.cancel('OperationvarCancelToken=axios.CancelToken;varsource=CancelToken.source();{cancelToken:}).catch(function(thrown){if(axios.isCancel(thrown)){console.log('Requestcanceled',}else}取消請(qǐng)求(message參數(shù)是可選的source.cancel('Operationcanceledbythe還可以通過(guò)傳遞一個(gè)executor函數(shù) 的構(gòu)造函數(shù)來(lái)創(chuàng)建cancelvarvarCancelToken=axios.CancelToken;varcancel;cancelToken:newCancelToken(functionexecutor(c)executor函數(shù)接收一個(gè)cancelcancel=NotecanceltokenUntilaxiosreaches release,breakingchangeswillbereleasedwithanewminorversion.

, willhavethesameAPI, willhavebreakingaxiosES6PromiseES6Promiseimportaxiosfrom'axios';importaxiosfrom'axios';UpgradeGuideCodeofConductaxiosisheavilyinspiredbythe$httpserviceprovidedinAngular. yaxiosisanefforttoprovide

-likeserviceforuseoutsideofUpgrade0.13.x->TheaxiosTypeScriptdefinitionshavebeenupdatedtomatchtheaxiosAPIandusetheES2015modulePleaseusethe statementtoimportaxiosinimportimportaxiosfrom'axios';.then(response=>.catch(error=>

Configconfigoptionhasbeen cedwithtwonew Pleaseusethem{{//DefineacustomagentforhttpAgent:newhttp.Agent({keepAlive:true//DefineacustomagentforhttpsAgent:newhttps.Agent({keepAlive:true}

Configconfigoptionhasbeenrecedwiththe

{{//DefineahandlerforuploadprogresseventsonUploadProgress:function(progressEvent){////DefineahandlerfordownloadprogresseventsonDownloadProgress:function(progressEvent){//}}0.12.x-> releasecontainsseveralchangestocustomadaptersanderrorErrorPrevioustothisreleaseanerrorcouldeitherbeaserverresponsewithbadstatuscodeoran.WiththisreleasePromisewillalwaysrejectwithan .Inthecasethataresponsewasreceived,the willalsoincludetheresponse..catch((error){console.log(error.message);console.log(error.code);//Notalwaysspecifiedconsole.log(error.config);//Theconfigthatwasusedtomaketherequestconsole.log(error.response);//OnlyavailableifresponsewasreceivedfromtheseRequestThisreleasechangesafewthingsabouthowrequestadapterswork.Pleasetakenoteifyouareusingyourowncustomadapter.ResponsetransformerisnowcalledoutsideofRequestadapterreturns Thismeansthatyounolongerneedto onresponsedata.YouwillalsolongerreceivePrevious

asargumentsinyourfunctionfunctionmyAdapter(resolve,reject,{varresponse={transformData(responseData,responseHeaders,status:request.status,statusText:request.statusText,headers:responseHeaderssettle(resolve,reject,}NewfunctionfunctionmyAdapter(config)returnnewPromise(function(resolve,{varresponse={data:responseData,status:request.status,statusText:request.statusText,headers:responseHeaderssettle(resolve,reject,}Seetherelatedcommitsformoredetails:Responsetransformers0.5.x->

releasecontainsmostlybugfixes,butthereareacouplethingstobeawareofES6PromiseUpuntilthe releaseES6 wasbeingpolyfilledusinges6-promise.Withthisrelease,thepolyfillhasbeenremoved,andyouwillneedtosupplyityourselfifyourenvironmentneedsit.varaxios=require('axios');Thiswillpolyfilltheglobalenvironment,andonlyneedstobedone/

溫馨提示

  • 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。

評(píng)論

0/150

提交評(píng)論