对 Java 后端的响应获取返回 null

我目前有一个反应按钮,它应该通过 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");


12345678_0001
浏览 252回答 2
2回答

萧十郎

您可以使用getInputStream()或getReader()读取请求的正文属性。这是完成工作的简单(非传统)方法:在标题中写入您的身体属性:    headers: {      "Content-Type": "application/text",      'question': this.state.value    }在后端,用于request.getHeader("question")获取值。

慕哥9229398

您可以尝试发送 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));
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java