Reputation: 910
Hi i am trying reactjs POST request through axios but getting error, I went through all docs but error is not solved.
Here is my error:
Uncaught (in promise) Error: Request failed with status code 400 at createError (eval at (bundle.js:4621), :15:15) at settle (eval at (bundle.js:4615), :18:12) at XMLHttpRequest.handleLoad (eval at (bundle.js:4609), :77:7)
Here is my Reactjs code:
import React from 'react';
import RaisedButton from 'material-ui/RaisedButton';
import TextField from 'material-ui/TextField';
import axios from 'axios';
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: 111,
studentName: 'param',
age: 24,
emailId: 2
};
data.append("myjsonkey", JSON.stringify(payload));
axios('http://localhost:8083/students',{
method: 'POST',
body: data,
headers: {
// 'Authorization': `bearer ${token}`,
'Content-Type': 'application/json'
}
})
.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>
);
}
}
Upvotes: 0
Views: 22536
Reputation: 1178
Please follow the bellow steps to resolve
First define the constructor
constructor(props) {
super(props);
this.state = {
id: ,
studentName: '',
age: ,
emailId: '',
};
write the handleSubmit Method
handleSubmit (evt) {
evt.preventDefault();
console.log("Submit");
const payload = {
id : this.state.id,
studentName : this.state.studentName,
age : this.state.age,
emailId : this.state.emailId,
}
axios({
method: 'post',
url: 'url-url',
data: payload,
}).then(function(response) {
console.log(response);
}).catch(function (error){
console.log(error);});
}
please don't forget bind the handleSubmit method
this.handleSubmit = this.handleSubmit.bind(this);
This will solved the problem
Upvotes: 0
Reputation: 341
var authOptions = {
method: 'post',
url: 'https://exam.com/apps',
data: JSON.stringify({"name": "ddd"});,
headers: {
'Content-Type': 'application/json'
},
json: true
};
axios(authOptions)
.then((response) => {
console.log(response);
})
.catch((error) => {
alert(error)
})
Upvotes: 1
Reputation: 7424
Checking axios api the correct way of making POST request is:
const payload = {
id: 111,
studentName: 'param',
age: 24,
emailId: 2
}
axios({
method: 'post',
url: '/user/12345',
data: payload, // you are sending body instead
headers: {
// 'Authorization': `bearer ${token}`,
'Content-Type': 'application/json'
},
})
Upvotes: 5
Reputation: 58523
There is no need of sending form data
var data = new FormData();
Just pass json to via axios ,
axios('http://localhost:8083/students',{
method: 'POST',
data : payload,
headers: {
// 'Authorization': `bearer ${token}`,
'Content-Type': 'application/json'
}
})
Simple way to do :
axios.post('http://localhost:8083/students',payload).then(...)
Upvotes: 1