解決axios post 后端無法接收數(shù)據(jù)的問題
更新時間:2019年10月29日 10:37:55 作者:HEI_RAO
今天小編就為大家分享一篇解決axios post 后端無法接收數(shù)據(jù)的問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
在后端 允許跨域的情況下,axios post 將數(shù)據(jù)傳到后端,但是后端無法接收,始終為空
請求能正常到達后端,那么肯定傳數(shù)據(jù)的地方的問題了
所以這里headers設置為
headers:{"Content-Type":'application/x-www-form-urlencoded;charset=UTF-8'}
另外transformRequest函數(shù)
transformRequest: [function (data) { // 對 data 進行任意轉換處理 let ret = ''; for (let it in data){ ret += encodeURIComponent(it)+'='+encodeURIComponent(data[it])+'&' } return ret // return this.$qs.stringify(data); }],
以下是全部代碼
this.$http.post('http://127.0.0.1:8123/addapp',{name:"test",desc:"test"},{ transformRequest: [function (data) { // 對 data 進行任意轉換處理 let ret = ''; for (let it in data){ ret += encodeURIComponent(it)+'='+encodeURIComponent(data[it])+'&' } return ret // return this.$qs.stringify(data); }], headers:{ "Content-Type":'application/x-www-form-urlencoded;charset=UTF-8' } }) .then(function (response) { console.log(response); }) .catch(function (error) { console.log(error); }); } else { this.$Message.error('Fail!'); } })
以上這篇解決axios post 后端無法接收數(shù)據(jù)的問題就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
Vue3+Vite+TS使用elementPlus時踩的坑及解決
這篇文章主要介紹了Vue3+Vite+TS使用elementPlus時踩的坑及解決方案,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-10-10