Axios

xiaoxiao2022-06-11  29

1.Axios简介


特性

从浏览器中创建 XMLHttpRequests从 node.js 创建 http 请求支持 Promise API拦截请求和响应转换请求数据和响应数据取消请求自动转换 JSON 数据客户端支持防御 XSRF

浏览器支持

谷歌浏览器火狐浏览器IE7及其以上Win10 EdgeSafari

安装

npm安装:

npm install axios

bower安装:

bower install axios

使用cdn:

<script src="https://unpkg.com/axios/dist/axios.min.js"></script>

2.Axios使用


使用Axios发送GET请求

1.直接在将请求参数拼接到请求路径上

// 为给定 ID 的 user 创建请求 axios.get('/user?ID=12345').then(function(response) { console.log(response); }).catch(function(error) { console.log(error); });

2.将参数放在请求列表

// 可选地,上面的请求可以这样做 axios.get('/user', { params: { ID: 12345 } }).then(function(response) { console.log(response); }).catch(function(error) { console.log(error); });

使用Axios发送POST请求

// axios发送post请求 axios.post('http://localhost:9090/post', { id: '123', name: '小明' }).then(function(response) { console.log(response); }).catch(function(error) { console.log(error); });

Axios执行并发请求

// 创建get请求 function getMu() { return axios.get('http://localhost:9090/get', { params: { id: 1, name: "小明" } }); } // 创建post请求 function postMu() { let param = new URLSearchParams(); param.append('id', '123'); param.append('name', '小明'); return axios.post('http://localhost:9090/post', param); } // 执行并发请求 axios.all([getMu(), postMu()]).then(axios.spread(function(getData, postData) { // 两个请求现在都执行完成 console.log(getData); console.log(postData); }));

Axios基础执行方法

待续

https://blog.csdn.net/huangpb123/article/details/78771077 https://www.jb51.net/article/109534.htm https://blog.csdn.net/qq_39702364/article/details/79753618 https://www.2cto.com/kf/201707/653004.html

转载请注明原文地址: https://www.6miu.com/read-4930310.html

最新回复(0)