我目前有一个反应按钮,它应该通过 post 将数据从状态发送到 servlet。服务器正在从 fetch 方法中得到命中,但数据似乎为空。 fetch( "http://localhost:8080/askQuestion", { method: "post", body: { question: this.state.value }, headers: { "Content-Type": "application/text" } }).then(console.log(this.state.value));我的请求对象中的正文和问题的值为空request.getParameter("question");request.getParameter("body");
2 回答
萧十郎
TA贡献1815条经验 获得超13个赞
您可以使用getInputStream()或getReader()读取请求的正文属性。
这是完成工作的简单(非传统)方法:
在标题中写入您的身体属性:
headers: {
"Content-Type": "application/text",
'question': this.state.value
}
在后端,用于request.getHeader("question")获取值。
慕哥9229398
TA贡献1877条经验 获得超6个赞
您可以尝试发送 json 数据作为body您的post请求
fetch('http://localhost:8080/askQuestion', {
method: 'post',
headers: {
'Accept': 'application/json, text/plain, */*',
'Content-Type': 'application/json'
},
body: JSON.stringify({ question: this.state.value })
}).then(res => res.json())
.then(res => console.log(res));
添加回答
举报
0/150
提交
取消