一, 什么是 axios
Axios 是一个基于 promise 的 HTTP 库, 可以用在浏览器和 node.JS 中
二, axios 的安装
1, 在项目根目录终端引入:
NPM isntall --save axios
2, 在使用 axios 的 JS 文件中加入:
import axios from 'axios';
三, axios 的使用
示例 1:get 请求
- // 为给定 ID 的 user 创建请求
- axios.get('/user?ID=12345')
- .then(function (response) {
- console.log(response);
- })
- .catch(function (error) {
- console.log(error);
- });
- // 可选地, 上面的请求可以这样做
- axios.get('/user', {
- params: {
- ID: 12345
- }
- })
- .then(function (response) {
- console.log(response);
- })
- .catch(function (error) {
- console.log(error);
- });
示例 2: 执行 post 请求
- axios.post('/user', {
- firstName: 'Fred',
- lastName: 'Flintstone'
- })
- .then(function (response) {
- console.log(response);
- })
- .catch(function (error) {
- console.log(error);
示例 3: 执行多个并发请求
- function getUserAccount() {
- return axios.get('/user/12345');
- }
- function getUserPermissions() {
- return axios.get('/user/12345/permissions');
- }
- axios.all([getUserAccount(), getUserPermissions()])
- .then(axios.spread(function (acct, perms) {
- // 两个请求现在都执行完成
- }));
更多 web 开发 https://www.html.cn/ 知识, 请查阅 HTML 中文网 !!
来源: http://www.css88.com/qa/react/15913.html