axios 基于HTTP客户端的浏览器和node.js
网址链接:https://github.com/axios/axios
1、特征:
- 制作,使XMLHttpRequest从浏览器
- 制作,使http来自node.js的请求
- 支持承诺API
- 拦截请求和响应
- 转换请求和响应数据
- 取消请求
- JSON数据的自动转换
- 客户端支持防止XSRF
2、安装:
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、配置使用:
post方法:
// 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、响应模式
请求的响应包含以下信息。
{ // `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: {} }
此处仅作交流学习,版权归原作者所有。
原文地址:https://www.cnblogs.com/w-yue/p/11792107.html
时间: 2024-10-09 02:24:00