这次给大家带来vue添加axios组件,解决post传参数为null,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的深浅拷贝使用详解
Vue中method与computed的区别
以上就是vue添加axios组件,解决post传参数为null的详细内容,更多请关注【创想鸟】其它相关文章!
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至253000106@qq.com举报,一经查实,本站将立刻删除。
发布者:PHP中文网,转转请注明出处:https://www.chuangxiangniao.com/p/2778794.html