1. 程式人生 > >axios post請求向後端提交資料

axios post請求向後端提交資料

axios向後端提交資料容易接收不到原因是傳參方式是request payload,引數格式是json,而並非用的是form傳參,所以在後臺用接收form資料的方式接收引數就接收不到了。post表單請求提交時,使用的Content-Type是application/x-www-form-urlencoded,而使用原生AJAX的POST請求如果不指定請求頭RequestHeader,預設使用的Content-Type是text/plain;charset=UTF-8。

所以採取以下解決辦法

安裝 qs : npm install qs --save 在頁面中引用 qs , 同時需要將請求頭headers改為: ‘Content-Type’: ‘application/x-www-form-urlencoded’,

       let postData=this.$qs.stringify(datas)
         this.$axios({
          method: 'post',
          url:'/api/productInfo/insert',
          data:postData
        }).then((res)=>{
            console.log(res)
        });