猿码叔叔 2019-05-26 16:22 采纳率: 50%
浏览 3154
已结题

VUE使用axios的post方式发送数据, 出现数据丢失问题

  • 请求头数据

**请求头数据**

  • 后台接收到的数据

**后台接收到的数据**

  • Vue代码
 this.commentReply = {
            id: this.uuid,
            javaId: java.id,
            comment: this.value.toString(),
            replier: '',
            commentator: this.username.toString(),
            avatar: '#f0000',
            createTime: moment().fromNow()
          }
          this.comments.push(this.commentReply);

                      saveComment () {
      console.log(this.commentReply)
      this.$axios.post(this.url.save, {commentReply: this.commentReply}).then((res) => {
        if (res.success) {
          this.loadData()
          console.log('评论成功')
        }
      })
    },
  • 后台代码
    @RequestMapping(value = "/saveComment", method = RequestMethod.POST)
    public Result<?> saveComment(@RequestBody CommentReply commentReply){
        boolean ok = this.replyService.saveComment(commentReply);
        log.info("..........要保存的评论" + commentReply.toString());
        Result<String> result = new Result<>();
        if(ok){
            result.setSuccess(true);
            result.setMessage("更新成功");
            return result;
        }
        result.setSuccess(false);
        result.setMessage("更新失败!");
        return result;
    }
  • 写回答

2条回答 默认 最新

  • 猿码叔叔 2019-05-27 13:51
    关注

    后台

    saveComment(@RequestBody CommentReply commentReply);
    

    前台

    this.$axios.post(this.url.save, {commentReply: this.commentReply}).then((res)
    

    问题我自己后来解决了:

    • 将前端发送数据的方法参数改为 > this.$axios.post(this.url.save, this.commentReply).then((res)... 然后就好了, 尽管之前那种写法没有报错,但是后台接收到的数据都是null值. 根据我的经验: 如果是传递非Json类型数据,比如String或Integer类型的可以用大括号囊括变量名和值,示例{id: '123'} 如果是Json对象类型,直接$axios.post(url, object名), 当然一定要和后台的requestBody或者requestParameter中的变量名一致
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

问题事件

  • 已结题 (查看结题原因) 2月7日

悬赏问题

  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?