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: {} }
此處僅作交流學習,版權歸原作者所有。