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

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日

悬赏问题

  • ¥30 Matlab打开默认名称带有/的光谱数据
  • ¥50 easyExcel模板 动态单元格合并列
  • ¥15 res.rows如何取值使用
  • ¥15 在odoo17开发环境中,怎么实现库存管理系统,或独立模块设计与AGV小车对接?开发方面应如何设计和开发?请详细解释MES或WMS在与AGV小车对接时需完成的设计和开发
  • ¥15 CSP算法实现EEG特征提取,哪一步错了?
  • ¥15 游戏盾如何溯源服务器真实ip?需要30个字。后面的字是凑数的
  • ¥15 vue3前端取消收藏的不会引用collectId
  • ¥15 delphi7 HMAC_SHA256方式加密
  • ¥15 关于#qt#的问题:我想实现qcustomplot完成坐标轴
  • ¥15 下列c语言代码为何输出了多余的空格