基于Promise的浏览器和node.js的HTTP 客户端
Features - 特征
- 从浏览器发送XMLHttpRequests
- 从node.js发送http请求
- 支持 Promise API
- 拦截请求及响应
- 改造请求和响应数据
- 取消请求
- 自动转换JSON数据
- 防范客户端XSRF
Installing
Using npm:
|
|
Using bower:
|
|
Using cdn:
|
|
Example
执行GET
请求
|
|
执行POST
请求
|
|
执行多个并发请求
|
|
axios API
请求可以通过相关设置axios
.
axios(config)
|
|
|
|
axios(url[, config])
|
|
请求方法的别名
为方便起见,已为所有支持的请求方法提供别名.
axios.request(config)
axios.get(url[, config])
axios.delete(url[, config])
axios.head(url[, config])
axios.options(url[, config])
axios.post(url[, data[, config]])
axios.put(url[, data[, config]])
axios.patch(url[, data[, config]])
注意
当使用别名方法url
,method
和data
属性不需要在配置中指定。
并发
处理并发请求的辅助函数。
axios.all(iterable)
axios.spread(callback)
创建一个实例
你可以用一个自定义的配置创建可信的一个新实例
axios.create([config])
|
|
实例方法
可用的实例方法如下。指定的配置将与实例配置合并。
axios#request(config)
axios#get(url[, config])
axios#delete(url[, config])
axios#head(url[, config])
axios#options(url[, config])
axios#post(url[, data[, config]])
axios#put(url[, data[, config]])
axios#patch(url[, data[, config]])
请求配置
这些是用于生成请求的可用配置选项。 只有 url
是必须的. 如果请求的 method
没有指定默认是GET
.
|
|
响应模式
一个请求的响应中包含以下信息。
|
|
当使用 then
, 您将收到如下的响应:
|
|
当使用 catch
, 或者通过一个 rejection callback 作为then
的第二个参数, 响应将获取 error
对象在处理错误部分做出解释.
配置默认
您可以指定将应用于每个请求的配置默认值
Axios全局默认值
|
|
自定义实例的默认值
|
|
优先级配置顺序
将按优先顺序合并配置。 该顺序为库默认值在 lib/defaults.js
里, then defaults
property of the instance, and finally config
argument for the request. The latter will take precedence over the former. Here’s an example.
|
|
Interceptors
You can intercept requests or responses before they are handled by then
or catch
.
|
|
If you may need to remove an interceptor later you can.
|
|
You can add interceptors to a custom instance of axios.
|
|
Handling Errors
|
|
You can define a custom HTTP status code error range using the validateStatus
config option.
|
|
Cancellation
You can cancel a request using a cancel token.
The axios cancel token API is based on the withdrawn cancelable promises proposal.
You can create a cancel token using the CancelToken.source
factory as shown below:
|
|
You can also create a cancel token by passing an executor function to the CancelToken
constructor:
|
|
Note: you can cancel several requests with the same cancel token.
Using application/x-www-form-urlencoded format
By default, axios serializes JavaScript objects to JSON
. To send data in the application/x-www-form-urlencoded
format instead, you can use one of the following options.
Browser
In a browser, you can use the URLSearchParams
API as follows:
|
|
Note that
URLSearchParams
is not supported by all browsers (see caniuse.com), but there is a polyfill available (make sure to polyfill the global environment).
Alternatively, you can encode data using the qs
library:
|
|
Node.js
In node.js, you can use the querystring
module as follows:
|
|
You can also use the qs
library.
Semver
Until axios reaches a 1.0
release, breaking changes will be released with a new minor version. For example 0.5.1
, and 0.5.4
will have the same API, but 0.6.0
will have breaking changes.
Promises
axios depends on a native ES6 Promise implementation to be supported.
If your environment doesn’t support ES6 Promises, you can polyfill.
TypeScript
axios includes TypeScript definitions.
|
|
Resources
Credits
axios is heavily inspired by the $http service provided in Angular. Ultimately axios is an effort to provide a standalone $http
-like service for use outside of Angular.