To encapsulate an ajax request=& gt; With a promise to encapsulate=& gt; Configure interceptor (interceptors)=& gt; To set up the instance the install method (vue plug-in form);
Nonsense not much said, as the code is as follows:
A/* and ajax encapsulation */
Const data={https://bbs.csdn.net/topics/}
_ajax (config, cb) {
Data. The config=config=utils. Merge (enclosing defaults, the config)
//1, to create the HTTP request instance
Let XHR=XMLHttpRequest? New XMLHttpRequest () : a new window. The ActiveXObject (' Microsoft. XMLHTTP)
//request intercept point 1, configurable request root domain name
If (config. The method. The toUpperCase ()==='GET' & amp; & The config. Data) {
The config. Url=config. Url + '? '+ utils. Serialize (config. Data)
config. Data=https://bbs.csdn.net/topics/null}
//2, open the request to equip
XHR. Open (config) method, the config url, config. The async)
If (isPOST (config. Method)) {
Config. Data=https://bbs.csdn.net/topics/JSON.stringify (config. Data)
XHR. SetRequestHeader (' the content-type ', 'application/json. Charset=utf-8 ')
}
//request intercept point 2, configurable request header parameter to carry
//3, send data
XHR. Send (config. Data)
//4, the response is complete callback to get data
XHR. Onreadystatechange=function () {
If (XHR. ReadyState===4) {
Data. The status=XHR. Status
Data. The XHR=XHR
If (isGetFullData (XHR)) {
Data. The data=https://bbs.csdn.net/topics/JSON.parse (XHR. The responseText)//data for the response object format
XHR=null
/* response response to intercept point 1, response success */
Cb (data)
} else {
/* response response to intercept point 2, response failed */
Cb (XHR. Status)
}
}
}
}
/* 2, encapsulated with promise (that encapsulates the get and post description) */
Get (url, data) {
The return of new Promise ((resolve, reject)=& gt; {
Const opt={
Url, data
}
_ajax (opt, resolve, reject)
})
}
Post (url, data and config) {
The return of new Promise ((resolve, reject)=& gt; {
Const opt={
Url, data, method: 'POST'
}
For (let k in config) {
Opt [k]=config [k].
}
_ajax (opt, resolve, reject)
})
}
The _ajax method to improve the above of course, are as follows:
_ajax (config, resolve, reject) {
Data. The config=config=utils. Merge (enclosing defaults, the config)
//1, to create the HTTP request instance
Let XHR=XMLHttpRequest? New XMLHttpRequest () : a new window. The ActiveXObject (' Microsoft. XMLHTTP)
//request intercept point 1, configurable request root domain name (called the open method need URL parameters, so the URL must be in front of the open call configuration)
If (config. The method. The toUpperCase ()==='GET' & amp; & The config. Data) {
The config. Url=config. Url + '? '+ utils. Serialize (config. Data)
config. Data=https://bbs.csdn.net/topics/null}
//2, open the request to equip
XHR. Open (config) method, the config url, config. The async)
If (isPOST (config. Method)) {
Config. Data=https://bbs.csdn.net/topics/JSON.stringify (config. Data)
XHR. SetRequestHeader (' the content-type ', 'application/json. Charset=utf-8 ')
}
//request intercept point 2, after calling the open method, can only be configuration request header parameter to carry
//3, send data
XHR. Send (config. Data)
Const self=this
//4, the response is complete callback to get data
XHR. Onreadystatechange=function () {
If (XHR. ReadyState===4) {
Data. The status=XHR. Status
Data. The XHR=XHR
If (isGetFullData (XHR)) {
Data. The data=https://bbs.csdn.net/topics/JSON.parse (XHR. The responseText)
XHR=null
/* response response to intercept point 1, response success */
If (self. Interceptors. Cb) {
/* this is a response to complete data interception point */
} else {
Resolve (data)
}
} else {
/* response response to intercept point 2, response failed */
If (self) interceptors) cbError) {
/* * response timeout interception point off (network or server error)/
} else {
Reject (new Error (' Request was unsuccessful: '+ XHR. Status))
}
}
}
}
}
Three, add an interceptor
Speaking of interceptor mainly 4 points,
Request: 1, before the open URL configuration;
2, after the open request header configuration and the request timeout configuration;
Response: the readyState===4, namely when the response is complete, interception of data, abnormal do further processing
Ok, continue to affix interceptor class code, as follows:
/* *
* interceptor class
*/
The class Interceptors {
Request={
Use (cb, cbError) {
Axios. Defaults=cb (axios. Defaults)
Typeof cbError==='function' & amp; & CbError (' error ')
}
}
Interceptor={readyState: 0}
The response={
Use (cb, cbError) {
If (typeof cb==='function') axios. Interceptors. Cb=true
If (typeof cbError==='function') axios. Interceptors. CbError=true
Axios. Cb=cb
Axios. CbError=cbError
If (interceptor & amp; & Interceptor. ReadyState===4) {
Let XHR=data. The XHR
If (isGetData (XHR)) {
Cb (data). Then ((res)=& gt; {
This. Resolve (res)
//data=https://bbs.csdn.net/topics/null
}). The catch ((error)=& gt; {
This. Reject (error)
})
} else {
CbError (XHR. Status). The catch ((error)=& gt; {
This. Reject (new Error (' Request was unsuccessful: '+ Error))
})
}
}
},
The run (resolve, reject) {
This. Resolve=resolve
This. Reject=reject
Interceptor. ReadyState=4
}
}
}
After add interceptors, _ajax methods have improved, but has about the same, as the ultimate code directly, as follows:
/* *
* tools
*/
Const utils={
/* *
* serialized data into: key=value& Key=value string
* @ param {*} o data object
*/
Serialize (o) {
Let the params=[]
For (in the let k o {
Params. Push (k + + o [k]) '='
}
nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull