我当前有一个react按钮,该按钮应该通过post将数据从状态发送到servlet。服务器正在从访存方法中命中,但数据似乎为空。
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");
答案 0 :(得分:0)
您可以使用 getInputStream()
或 getReader()
来读取请求的正文属性。
这是完成工作的一种简单(非传统)方法:
在标题中写入您的正文属性:
headers: {
"Content-Type": "application/text",
'question': this.state.value
}
在后端,使用 request.getHeader("question")
获取值。
答案 1 :(得分:0)
您可以尝试将 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));