I am trying to send some data to api via post request.
Frontend Code (React)
handleSubmit = e => {
e.preventDefault();
fetch("http://localhost:9000", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({
title: this.state.title,
text: this.state.text
})
});
};
Server (Express)
var urlencodedParser = bodyParser.urlencoded({ extended: false });
app.post("/", urlencodedParser, function(req, res) {
sql = "INSERT INTO POSTS (id, title, text) VALUES ?";
var values = [[uuidv1().toString(), req.body.title, req.body.text]];
con.query(sql, [values], function(err, result, fields) {
if (err) throw err;
});
});
When I'm submitting the form, I'm getting this error message from developer console
I am running client side on localhost:3000
and server side on localhost:9000
. Can someone let me know how this problem occurs and how can I fix it?