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

Sp *_*aju 4 post fetch reactjs

我正在尝试通过反应进行 POST 请求调用,但出现错误。如果有人知道帮助我,请帮助我我必须更改的地方。

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

这是我的反应代码:

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>


    );
  }


}
Run Code Online (Sandbox Code Playgroud)

RIY*_*HAN 7

body缺少从取#职务的请求。

bodyFormData在你的情况下应该是实例。或者可以是其他类型的实例,如ArrayBufferBlob/File..等。

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
})
Run Code Online (Sandbox Code Playgroud)

为了更多的你Fetch