axios HTTP-based client browser and node.js
Website link: https: //github.com/axios/axios
1, features:
- Production, the XMLHttpRequest from the browser
- Production, the http requests from the node.js
- Support promised API
- Intercept request and response
- Conversion request and response data
- Cancellation request
- Automatically convert data JSON
- Client support to prevent XSRF
2, the installation:
Using npm:
$ npm install axios
Using bower:
$ bower install axios
Using yarn:
$ Yarn add axios
Using cdn:
<script src="https://unpkg.com/axios/dist/axios.min.js"></script>
3, configured to use:
post method:
// Send a POST request
axios({
method: 'post', url: '/user/12345', data: { firstName: 'Fred', lastName: 'Flintstone' } });
get方法:
// GET request for remote image
axios({
method: 'get', url: 'http://bit.ly/2mTM3nY', responseType: 'stream' }) .then(function (response) { response.data.pipe(fs.createWriteStream('ada_lovelace.jpg')) });
4, response mode
In response to the request contains the following information.
{
// `data` is the response that was provided by the server
data: {},
// `status` is the HTTP status code from the server response
status: 200, // `statusText` is the HTTP status message from the server response statusText: 'OK', // `headers` the headers that the server responded with // All header names are lower cased headers: {}, // `config` is the config that was provided to `axios` for the request config: {}, // `request` is the request that generated this response // It is the last ClientRequest instance in node.js (in redirects) // and an XMLHttpRequest instance in the browser request: {} }
Here only for the exchange of learning, belongs to original author.