vue.js用vue-resource传值遇到的问题

1.最近在学vue,刚接触到vue这块,本文主要介绍从vue像后台传值的问题
使用vue-resource发送请求时,在params中传一个对象,后台实体类接受,但总是失败,后台得到的值是空,错误代码如下:`

前端代码
new Vue({
    el:"#app",
    data: {
            book:{
                id:'',
                author:'',
                name1:'',
                price:''
            },
            books: []
    },
    created:function(){//在创建实例之后调用
        let that = this;
        axios.post('demoController/show.do'
        )
        .then(function (res) {
            that.changeBooks(res.data);
        })
        .catch(function (error) {
            console.log(error);
        })
    },
    methods:{
        add:function(){
            this.books.push(this.book);
            //console.log(JSON.stringify(this.book));
            this.$http.post('demoController/add.do',
                {
                   bookBean:this.book 
                },
                {emulateJSON:true})
                .then(function(res){
                    console.log(res);
                },function(){
                    console.log('请求失败处理');
                })
            this.book={id:'', author:'', name1:'', price:''};
        },`
        后台部分:` 
      @ResponseBody
    @RequestMapping("/add.do")
    public String add(BookBean bookBean){
        int result = bookService.insert(bookBean);
        return JSON.toJSONString(result);
    }`
    更改后传值时,参数要和bean中的属性相对应
更改后代码:
 methods:{
    add:function(){
        this.books.push(this.book);
        //console.log(JSON.stringify(this.book));
        this.$http.post('demoController/add.do',
            {
                id:this.book.id,
                author:this.book.author,
                name1:this.book.name1,
                price:this.book.price
            },
            {emulateJSON:true})
            .then(function(res){
                console.log(res);
            },function(){
                console.log('请求失败处理');
            })
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值