我试图通过react进行POST请求调用,但我正在让任何人都知道的error.If帮助我,并请在我需要更改的地方帮助我。
错误为:{时间戳: 1510396949738,状态: 415,错误:“不支持的媒体异常:字符集消息:”内容类型‘multipart/form-data;"org.springframework.web.HttpMediaTypeNotSupportedException",=-Web…daryTY6125I1exH8Ry7f;charset=UTF-8’不支持“,……}
下面是我的React代码:
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>
    );
  }
}发布于 2017-11-11 17:39:11
fetch#post请求中缺少body。
body应该是case.Or中FormData的实例,也可以是ArrayBuffer、Blob/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
})如需更多信息,请使用Fetch。
https://stackoverflow.com/questions/47236410
复制相似问题