状态:415,错误:"不支持的媒体类型" - 针对POST请求的Reactjs

时间:2017-11-12 11:33:52

标签: java reactjs post http-post

我正在尝试通过reactjs发出POST请求,但是我收到了一个错误。 GET的相同代码工作正常。

错误是:

  

状态:415,错误:"不支持的媒体类型",例外:" org.springframework.web.HttpMediaTypeNotSupportedException",

这是我的reactjs代码:

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;
   const payload = {
   id: self.refs.id.getValue(),
   studentName: self.refs.sname.getValue(),
   age: self.refs.age.getValue(),
   emailId: self.refs.emailId.getValue()
};
   var data = new FormData();
data.append("myjsonkey", JSON.stringify(payload));

fetch('http://localhost:8083/students', {
   method: 'POST',
   dataType: 'jsonp',
   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>

   );
 }
}

0 个答案:

没有答案