如何排查并解决axios POST请求后端接收不到数据的问题?
- 内容介绍
- 文章标签
- 相关推荐
本文共计354个文字,预计阅读时间需要2分钟。
在跨域环境下,使用axios通过POST方法将数据传送到后端,但后端无法接收数据。最初结果为空,请求能正常到达后端。因此,需要确定数据传输方式的问题。可以尝试在请求的headers中设置`Content-Type`,例如:headers: { Content-Type: 'application/json' }。
在后端 允许跨域的情况下,axios post 将数据传到后端,但是后端无法接收,始终为空
请求能正常到达后端,那么肯定传数据的地方的问题了
所以这里headers设置为
headers:{"Content-Type":'application/x-www-form-urlencoded;charset=UTF-8'}
另外transformRequest函数
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.$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 后端无法接收数据的问题就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持易盾网络。
本文共计354个文字,预计阅读时间需要2分钟。
在跨域环境下,使用axios通过POST方法将数据传送到后端,但后端无法接收数据。最初结果为空,请求能正常到达后端。因此,需要确定数据传输方式的问题。可以尝试在请求的headers中设置`Content-Type`,例如:headers: { Content-Type: 'application/json' }。
在后端 允许跨域的情况下,axios post 将数据传到后端,但是后端无法接收,始终为空
请求能正常到达后端,那么肯定传数据的地方的问题了
所以这里headers设置为
headers:{"Content-Type":'application/x-www-form-urlencoded;charset=UTF-8'}
另外transformRequest函数
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.$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 后端无法接收数据的问题就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持易盾网络。

