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"
      },

  


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM