为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

12345678_0001 2021-09-12 16:56:45
我目前有一个反应按钮,它应该通过 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")获取值。


查看完整回答
反对 回复 2021-09-12
?
慕哥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));


查看完整回答
反对 回复 2021-09-12
  • 2 回答
  • 0 关注
  • 251 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信