I've been attempting to make a POST request using the fetch
method in reactjs. Despite going through some documentation, I haven't been able to resolve my error. Can someone please lend a hand?
Below is the snippet of my reactjs code:
handleSubmit(e) {
e.preventDefault();
var self = this;
const payload = {
id: 111,
studentName: 'param',
age: 24,
emailId: 2
};
fetch({
method: 'POST',
url: 'http://localhost:8083/students',
body: payload,
headers: {
'Content-Type': 'application/json',
'Accept': 'application/json'
}
})
.then(function(response) {
return response.json()
}).then(function(body) {
console.log(body);
});
}
}
If there's anyone familiar with reactjs, could you provide a straightforward example on how to execute a post request? It can be done using either fetch
or axios
.