axios配置对象详细说明

清疚 2022-10-31 04:08 238阅读 0赞

axios官方文档配置说明

  1. 本文主要针对GitHub上的axios的配置文档做一个详解的介绍和说明,并对常用的参数做一个提取说明。

axios-GitHub链接地址–内含使用方法以及配置详解介绍

#
  1. 高频常用参数罗列:
  2. 1url // 通过设置url参数,决定请求到底发送给谁
  3. 2method // 设置请求的类型,get/post/delete..
  4. 3baseURL // 设置url的基础结构,发送请求配置时只需要设置url即可,axios会自动将两者进行拼接
  5. 4headers // 头信息:比较实用的参数,在某些项目当中,进行身份校验的时候,要求在头信息中加入一个特殊的 标识 // 来检验请求是否满足要求,可以借助headers对请求头信息做一个配置
  6. 5params // 也是一个比较常用的参数,来设定url参数的,可以通过params直接添加url参数名和参数值
  7. 6data
  8. 7timeout // 超时请求时间,单位是ms 超过请求时间,请求就会被取消
  9. 8:其余的都是不经常使用的参数,了解即可!
默认配置设置:
  1. 还是有很多其他的默认设置可以进行配置:
  2. // 设置默认配置
  3. axios.defaults.method='GET'; //设置默认的请求类型是 GET
  4. axios.defaults.baseURL='http://localhost:3000'; //设置基础URL
  5. axios.defaults.params={ id:100};
  6. axios.defaults.timeout=3000;

在这里插入图片描述

官方配置文档详解:
  1. {
  2. // `url` is the server URL that will be used for the request
  3. // 通过设置url参数,决定请求到底发送给谁
  4. url: '/user',
  5. // `method` is the request method to be used when making the request
  6. // 设置请求的类型,get/post/delete..
  7. method: 'get', // default
  8. // `baseURL` will be prepended to `url` unless `url` is absolute.
  9. // It can be convenient to set `baseURL` for an instance of axios to pass relative URLs
  10. // to methods of that instance.
  11. // 设置url的基础结构,发送请求配置时只需要设置url即可,axios会自动将两者进行拼接
  12. baseURL: 'https://some-domain.com/api/',
  13. // `transformRequest` allows changes to the request data before it is sent to the server
  14. // This is only applicable for request methods 'PUT', 'POST', 'PATCH' and 'DELETE'
  15. // The last function in the array must return a string or an instance of Buffer, ArrayBuffer,
  16. // FormData or Stream
  17. // You may modify the headers object.
  18. // 可以对请求的数据做一个处理,在将处理完的结果向服务器发送
  19. transformRequest: [function (data, headers) {
  20. // Do whatever you want to transform the data
  21. return data;
  22. }],
  23. // `transformResponse` allows changes to the response data to be made before
  24. // it is passed to then/catch
  25. // 对响应结果做一个处理配置
  26. transformResponse: [function (data) {
  27. // Do whatever you want to transform the data
  28. return data;
  29. }],
  30. // `headers` are custom headers to be sent
  31. // 头信息:比较实用的参数,在某些项目当中,进行身份校验的时候,要求在头信息中加入一个特殊的标识
  32. // 来检验请求是否满足要求,可以借助headers对请求头信息做一个配置
  33. headers: { 'X-Requested-With': 'XMLHttpRequest'},
  34. // `params` are the URL parameters to be sent with the request
  35. // Must be a plain object or a URLSearchParams object
  36. // 也是一个比较常用的参数,来设定url参数的,可以通过params直接添加url参数名和参数值
  37. params: {
  38. ID: 12345
  39. },
  40. // `paramsSerializer` is an optional function in charge of serializing `params`
  41. // (e.g. https://www.npmjs.com/package/qs, http://api.jquery.com/jquery.param/)
  42. // 参数序列化的,不经常使用,对请求的参数做一个系列化,转化成字符串
  43. paramsSerializer: function (params) {
  44. return Qs.stringify(params, { arrayFormat: 'brackets'})
  45. },
  46. // `data` is the data to be sent as the request body
  47. // Only applicable for request methods 'PUT', 'POST', 'DELETE , and 'PATCH'
  48. // When no `transformRequest` is set, must be of one of the following types:
  49. // - string, plain object, ArrayBuffer, ArrayBufferView, URLSearchParams
  50. // - Browser only: FormData, File, Blob
  51. // - Node only: Stream, Buffer
  52. data: {
  53. firstName: 'Fred'
  54. },
  55. // syntax alternative to send data into the body
  56. // method post
  57. // only the value is sent, not the key
  58. data: 'Country=Brasil&City=Belo Horizonte',
  59. // `timeout` specifies the number of milliseconds before the request times out.
  60. // If the request takes longer than `timeout`, the request will be aborted.
  61. // 超时请求时间,单位是ms 超过请求时间,请求就会被取消
  62. timeout: 1000, // default is `0` (no timeout)
  63. // `withCredentials` indicates whether or not cross-site Access-Control requests
  64. // should be made using credentials
  65. // 跨域请求时对cookie的携带做一个设置,false为不携带
  66. withCredentials: false, // default
  67. // `adapter` allows custom handling of requests which makes testing easier.
  68. // Return a promise and supply a valid response (see lib/adapters/README.md).
  69. // 发送请求识别器做一个设置
  70. adapter: function (config) {
  71. /* ... */
  72. },
  73. // `auth` indicates that HTTP Basic auth should be used, and supplies credentials.
  74. // This will set an `Authorization` header, overwriting any existing
  75. // `Authorization` custom headers you have set using `headers`.
  76. // Please note that only HTTP Basic auth is configurable through this parameter.
  77. // For Bearer tokens and such, use `Authorization` custom headers instead.
  78. // 请求基础验证,设置用户名和密码的,相对用的较少
  79. auth: {
  80. username: 'janedoe',
  81. password: 's00pers3cret'
  82. },
  83. // `responseType` indicates the type of data that the server will respond with
  84. // options are: 'arraybuffer', 'document', 'json', 'text', 'stream'
  85. // browser only: 'blob'
  86. // 对响应体结果的格式做个设置
  87. responseType: 'json', // default
  88. // `responseEncoding` indicates encoding to use for decoding responses (Node.js only)
  89. // Note: Ignored for `responseType` of 'stream' or client-side requests
  90. responseEncoding: 'utf8', // default
  91. // `xsrfCookieName` is the name of the cookie to use as a value for xsrf token
  92. xsrfCookieName: 'XSRF-TOKEN', // default
  93. // `xsrfHeaderName` is the name of the http header that carries the xsrf token value
  94. xsrfHeaderName: 'X-XSRF-TOKEN', // default
  95. // `onUploadProgress` allows handling of progress events for uploads
  96. // browser only
  97. onUploadProgress: function (progressEvent) {
  98. // Do whatever you want with the native progress event
  99. },
  100. // `onDownloadProgress` allows handling of progress events for downloads
  101. // browser only
  102. onDownloadProgress: function (progressEvent) {
  103. // Do whatever you want with the native progress event
  104. },
  105. // `maxContentLength` defines the max size of the http response content in bytes allowed in node.js
  106. maxContentLength: 2000,
  107. // `maxBodyLength` (Node only option) defines the max size of the http request content in bytes allowed
  108. maxBodyLength: 2000,
  109. // `validateStatus` defines whether to resolve or reject the promise for a given
  110. // HTTP response status code. If `validateStatus` returns `true` (or is set to `null`
  111. // or `undefined`), the promise will be resolved; otherwise, the promise will be
  112. // rejected.
  113. validateStatus: function (status) {
  114. return status >= 200 && status < 300; // default
  115. },
  116. // `maxRedirects` defines the maximum number of redirects to follow in node.js.
  117. // If set to 0, no redirects will be followed.
  118. maxRedirects: 5, // default
  119. // `socketPath` defines a UNIX Socket to be used in node.js.
  120. // e.g. '/var/run/docker.sock' to send requests to the docker daemon.
  121. // Only either `socketPath` or `proxy` can be specified.
  122. // If both are specified, `socketPath` is used.
  123. socketPath: null, // default
  124. // `httpAgent` and `httpsAgent` define a custom agent to be used when performing http
  125. // and https requests, respectively, in node.js. This allows options to be added like
  126. // `keepAlive` that are not enabled by default.
  127. httpAgent: new http.Agent({ keepAlive: true }),
  128. httpsAgent: new https.Agent({ keepAlive: true }),
  129. // `proxy` defines the hostname, port, and protocol of the proxy server.
  130. // You can also define your proxy using the conventional `http_proxy` and
  131. // `https_proxy` environment variables. If you are using environment variables
  132. // for your proxy configuration, you can also define a `no_proxy` environment
  133. // variable as a comma-separated list of domains that should not be proxied.
  134. // Use `false` to disable proxies, ignoring environment variables.
  135. // `auth` indicates that HTTP Basic auth should be used to connect to the proxy, and
  136. // supplies credentials.
  137. // This will set an `Proxy-Authorization` header, overwriting any existing
  138. // `Proxy-Authorization` custom headers you have set using `headers`.
  139. // If the proxy server uses HTTPS, then you must set the protocol to `https`.
  140. proxy: {
  141. protocol: 'https',
  142. host: '127.0.0.1',
  143. port: 9000,
  144. auth: {
  145. username: 'mikeymike',
  146. password: 'rapunz3l'
  147. }
  148. },
  149. // `cancelToken` specifies a cancel token that can be used to cancel the request
  150. // (see Cancellation section below for details)
  151. cancelToken: new CancelToken(function (cancel) {
  152. }),
  153. // `decompress` indicates whether or not the response body should be decompressed
  154. // automatically. If set to `true` will also remove the 'content-encoding' header
  155. // from the responses objects of all decompressed responses
  156. // - Node only (XHR cannot turn off decompression)
  157. decompress: true // default
  158. }

发表评论

表情:
评论列表 (有 0 条评论,238人围观)

还没有评论,来说两句吧...

相关阅读

    相关 axios配置对象详细说明

    axios官方文档配置说明 本文主要针对GitHub上的axios的配置文档做一个详解的介绍和说明,并对常用的参数做一个提取说明。 [axios-GitHub链接地