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

遇到的问题描述 :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"
      },

  

posted @ 2019-04-09 16:17  雨天呐  阅读(8965)  评论(0编辑  收藏  举报