首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何通过react.js发起POST请求?

如何通过react.js发起POST请求?
EN

Stack Overflow用户
提问于 2017-11-11 17:32:19
回答 1查看 23.8K关注 0票数 4

我试图通过react进行POST请求调用,但我正在让任何人都知道的error.If帮助我,并请在我需要更改的地方帮助我。

错误为:{时间戳: 1510396949738,状态: 415,错误:“不支持的媒体异常:字符集消息:”内容类型‘multipart/form-data;"org.springframework.web.HttpMediaTypeNotSupportedException",=-Web…daryTY6125I1exH8Ry7f;charset=UTF-8’不支持“,……}

下面是我的React代码:

代码语言:javascript
运行
复制
import React from 'react';
import RaisedButton from 'material-ui/RaisedButton';
import TextField from 'material-ui/TextField';
const style = {
  margin: 15,
marginLeft: 600
};
export default class  Register extends React.Component {
  constructor(props) {
    super(props);
    this.onSubmit=this.handleSubmit.bind(this);
}
handleSubmit(e) {
    e.preventDefault();
    var self = this;


    var data = new FormData();
    const payload = {
    id: self.refs.id.getValue(),
    studentName: self.refs.sname.getValue(),
    age: self.refs.age.getValue(),
    emailId: self.refs.emailId.getValue()
};
data.append("myjsonkey", JSON.stringify(payload));

fetch('http://localhost:8083/students/', {
    method: 'POST',
    headers: {
    'Accept': 'application/json'
  },
    body: data
  })
    .then(function(response) {
        return response.json()
      }).then(function(body) {
        console.log(body);
      });
  }

render() {
    return (
      <form onSubmit={this.onSubmit}>
      <div style={style}>
      <TextField ref='id'
      hintText="Enter Student id"
      floatingLabelText="id"
      />
      <br/>
      <TextField ref='sname'
      hintText="Enter your Last Name"
      floatingLabelText="StudentName"
      />
      <br/>
      <TextField ref='age'
      hintText="Enter your Age"
      floatingLabelText="age"
      />
      <br/>

      <TextField ref='emailId'
      hintText="Enter your Email"
      floatingLabelText="emailId"
      />
      <br/>
      <br/>
      <input type="submit" />


      </div>
          </form>


    );
  }


}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-11-11 17:39:11

fetch#post请求中缺少body

body应该是case.Or中FormData的实例,也可以是ArrayBufferBlob/File等其他类型的实例。等。

代码语言:javascript
运行
复制
var data = new FormData();
const payload = {
    id: self.refs.id,
    studentName: self.refs.sname,
    age: self.refs.age,
    emailId: self.refs.emailId

};
data.append("myjsonkey", JSON.stringify(payload));

fetch('http://localhost:8083/students/', {
    method: 'POST',
    body: data
})

如需更多信息,请使用Fetch

票数 7
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/47236410

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档