码迷,mamicode.com
首页 > 移动开发 > 详细

vue中axios发送post请求,后端(@RequestParam)接不到参数

时间:2019-04-09 16:38:19      阅读:566      评论:0      收藏:0      [点我收藏+]

标签:method   hex   cat   传递   class   ati   form   post请求   直接   

遇到的问题描述 :axios post 请求,后端接收不到参数。

        我们的接口是java,用@RequestParam来接收前端的参数

解决方案:使用qs;axios中已经包含有qs,所以无需重新安装,直接引入就好

import Qs from ‘qs‘//引入qs
   let chedata = {
      data: encStr,
      sign: md5.hexMD5(che),
      timestamp: timestamp,
    }
//chedata是我需要传递给后端的参数
    console.log(Qs.stringify(chedata))
    axios({
      header: {
        "Content-Type": "application/x-www-form-urlencoded;charset=utf-8"
      },
      method:method || get,
      url: baseUrl + url,
      data:Qs.stringify(chedata),//在传参之前先用qs.stringify转化一下格式
      responseType
    }).then((response) => {
      console.log(response)
      success(response.data);
    }).catch((err)=>{
      console.log(err)
    })
  }
}

  网上很多解决方案里面说还需要把请求头替换一下,但是我试了一下,替换和不替换好像没有影响;

如果需要替换的话,就将header替换为‘Content-Type‘:‘application/x-www-form-urlencoded‘

header: {
        "Content-Type": "application/x-www-form-urlencoded;charset=utf-8"
      },

  

vue中axios发送post请求,后端(@RequestParam)接不到参数

标签:method   hex   cat   传递   class   ati   form   post请求   直接   

原文地址:https://www.cnblogs.com/yutianA/p/10677677.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!