axios ------ configuration in detail

Reprinted from: https://blog.csdn.net/chjj0904/article/details/79086746

Axios configuration parameters lot, we understand one by one

url - url for sending a request to the server
method - request method GET method default
baseURL - base URL path, url if not absolute, as https://some-domain.com/api/v1/login ? name = jack, then sends a request to the server URL will be baseURL + url.
transformRequest - transformRequest method allows to modify the request before sent to the server, this method applies only to PUT, POST, and PATCH methods. Moreover, this last method must return a string, ArrayBuffer or Stream.
transformResponse - transformResponse method allows to modify the response data is transmitted before then / catch data. Finally, this method should return data.
headers - Headers send custom headers, header file contains various information http request.
params - params query parameter object transmission request, data objects may be spliced into url param1 = value1 & param2 = value2 ?.
paramsSerializer - params parameter sequence gasifier.
data - data is transmitted in data object POST, PUT or PATCH request.
timeout - timeout setting request milliseconds
withCredentials - indicates whether there is need to use cross-domain requests a certificate
adapter - adapter allows the user to more easily test the processing request. It returns a Promise and an effective response
auth - auth indicates that provide credentials for complete identity http authentication. This will set up a Authorization authorization information in the headers. Custom Authorization Authorization To set up headers in.
responseType - Indicates that the server returns the data type of response, there arraybuffer, blob, document, json, text, stream six types, the default is json similar data.
xsrf token as a cookie name value - xsrfCookieName
xsrfHeaderName - HTTP head with the name xsrf token value
onUploadProgress - allowed to do some of the operations during the upload process of
onDownloadProgress - allowed to do some of the operations during the download of
maxContentLength - defined the maximum length of the response received by the response data.
validateStatus - validateStatus promise defines whether to accept or deny access in response to an HTTP status code decision. If validateStatus returns true (or set to null or undefined), promise to be received; otherwise, promise to be rejected.
maxRedirects - maxRedirects defines the maximum in node.js redirect, if set to 0, no redirect.
httpAgent - defined at http request using the proxy
httpsAgent - when the agent is defined using https request
proxy - defined proxy host name and port of the proxy server, auth
cancelToken - cancelToken define a cancel token request for cancellation

Config configuration unified
interface testing, we often need to switch the online environment and test environment, where we can all be configured via Config, so that all of our requests are initiated by this basic URL away.
= 5000 axios.defaults.timeout;
axios.defaults.headers.post [ 'the Type-the Content'] = 'file application / X-WWW-form-urlencoded; charset = UTF-. 8';
axios.defaults.baseURL = 'HTTP: //www.xxxx.xxx/api ';
// axios.defaults.baseURL =' http://192.168.1.129:8383 ';
interceptors interceptors
here I must highlight, at the time we launched a large number of requests, the need for uniform treatment would make a request to use it. I personally comparing After using vue-resource and axios, axios configuration more human.
The official presentation of this API

You can intercept requests or responses before they are handled by then or catch.
Before you can block request or response, their processing operation or abnormal

unitary operation request
if the request is a POST, the configuration can not use the params field, data field required.
There is a small caveats, POST pass parameters need to be serialized, or the server is not received correctly, oh, I will complain. So here we have to conduct a request for data serialization.

Request method alias
all requests following method is supported axios alias, to facilitate various requests. NOTE: Data [...] The representation may be empty. url is ajax request address; data submitted data objects; config configuration object is, all configurations can be implemented in ajax config.

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]])
concurrency
following interfaces concurrent requests for processing (a plurality of simultaneously processing a plurality of request)

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

Published 23 original articles · won praise 7 · views 20000 +

Guess you like

Origin blog.csdn.net/ghd602338792/article/details/96858155