下面我就为大家分享一篇基于vue 添加axios组件,解决post传参数为null的问题,具有很好的参考价值,希望对大家有所帮助。
好,下面上货。
1、安装axios
- npm install axios --save
登录后复制
2、添加axios组件
- import axios from 'axios'axios.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded';axios.defaults.baseURL = 'http://localhost:7878/zkview';Vue.prototype.$ajax = axios;
登录后复制
3、get请求
立即学习“前端免费学习笔记(深入)”;
- testGet: function () { this.$ajax({ method: 'get', url: '/test/greeting', params: { firstName: 'Fred', lastName: 'Flintstone' } }).then(function (response) { console.log(response); }).catch(function (error) { console.log(error); });},
登录后复制
4、post请求
- testPost: function () { var params = new URLSearchParams(); params.append('name', 'hello jdmc你好'); params.append('id', '2'); this.$ajax({ method: 'post', url: '/test/greeting2', data:params// data: {id: '3', name: 'abc'} }).then(function (response) { console.log(response); }).catch(function (error) { console.log(error); }) }
登录后复制
5、运行结果:
6、注意:
在使用post方式的时候传递参数有两种方式,一种是普通方式,一种是json方式,如果后台接受的是普通方式,那么使用上述方式即可。
普通的formed方式
- var params = new URLSearchParams();params.append('name', 'hello jdmc你好');params.append('id', '2');data:params
登录后复制
后台接收参数:
- public Student greeting2(int id,String name) {
登录后复制
json方式
- data: {id: '3', name: 'abc'}
登录后复制
后台接收参数
- public Object greeting2(@RequestBody Object student) {
登录后复制
上面是我整理给大家的,希望今后会对大家有帮助。
相关文章:
聊聊JS动画库 Velocity.js的使用
vue 过滤器filter实例详解
Vue.js 动态为img的src赋值方法
以上就是通过vue实现添加axios组件,解决post传参数为null方面的问题(详细教程)的详细内容,更多请关注【创想鸟】其它相关文章!
声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。