VUE常见问题 - AXIOS使用请求
2020-02-05 18:56
489 查看
发起一个get请求
[code]<input id="get01Id" type="button" value="get01"/> <script> $("#get01Id").click(function () { axios.get('http://localhost:8080/user/findById?id=1') .then(function (value) { console.log(value); }) .catch(function (reason) { console.log(reason); }) }) </script>
另外一种形式:
[code]<input id="get02Id" type="button" value="get02"/> <script> $("#get02Id").click(function () { axios.get('http://localhost:8080/user/findById', { params: { id: 1 } }) .then(function (value) { console.log(value); }) .catch(function (reason) { console.log(reason); }) }) </script>
发起一个post请求
在官方文档上面是这样的:
[code]axios.post('/user', { firstName: 'Fred', lastName: 'Flintstone' }).then(function (res) { console.log(res); }).catch(function (err) { console.log(err); });
但是如果这么写,会导致后端接收不到数据
所以当我们使用post请求的时候,传递参数要这么写:
[code]<input id="post01Id" type="button" value="post01"/> <script> $("#post01Id").click(function () { var params = new URLSearchParams(); params.append('username', 'sertyu'); params.append('password', 'dfghjd'); axios.post('http://localhost:8080/user/addUser1', params) .then(function (value) { console.log(value); }) .catch(function (reason) { console.log(reason); }); }) </script>
行多个并发请求
[code]<input id="button01Id" type="button" value="点01"/> <script> function getUser1() { return axios.get('http://localhost:8080/user/findById?id=1'); } function getUser2() { return axios.get('http://localhost:8080/user/findById?id=2'); } $("#buttonId").click(function () { axios.all([getUser1(), getUser2()]) .then(axios.spread(function (user1, user2) { alert(user1.data.username); alert(user2.data.username); })) }) </script>
另外一种形式:
[code]<input id="button02Id" type="button" value="点02"/> <script> $("#button02Id").click(function () { axios.all([ axios.get('http://localhost:8080/user/findById?id=1'), axios.get('http://localhost:8080/user/findById?id=2') ]) .then(axios.spread(function (user1, user2) { alert(user1.data.username); alert(user2.data.username); })) }) </script>
原文连接: https://blog.csdn.net/qq_41033290/article/details/82844716
- 点赞
- 收藏
- 分享
- 文章举报
相关文章推荐
- vue-cli的axios使用方法,get、post请求的跨域问题解决
- 使用vue-axios请求geoJson数据报错的问题
- vue.js中使用axios数据请求遇到的问题这篇文章帮你妥妥的解决
- vue 使用axios 跨域请求数据的问题
- Vue 使用axios 发送post请求,参数格式有错误问题
- 详解vue中使用axios对同一个接口连续请求导致返回数据混乱的问题
- Vue中使用axios(POST请求)调用后端接口时,后端接口接收不到前端参数的问题
- Vue中使用axios.post请求传参后端拿不到值问题
- vue使用axios跨域请求数据问题详解
- vuejs中vue-resource或使用axios发送请求及解决跨域问题
- vue 使用axios 跨域请求数据的问题
- 使用vue-cli脚手架 与 axios 请求数据 配置代理进行跨域访问数据问题
- Vue2.0总结———vue使用过程常见的一些问题
- vue使用axios调用豆瓣API跨域问题
- vue使用axios来请求本地json文件
- vue中使用axios请求接口,请求会发送两次
- vue中使用vue-resource、axios、fetch-jsonp请求数据
- Vue.js实战之使用Vuex + axios发送请求详解
- vue使用过程常见的一些问题
- 使用 Vuex + axios 发送请求