Express does not receive parameters via POST from React using Fetch API
If your production environment will host both the frontend and server, your requests eventually won't have to deal with CORS. In this case, I would suggest targeting Port 3000 in your frontend react Fetch request, and using the "proxy" feature Proxying API Requests in Development on your Frontend server for development.
Basically, put this in your package.json
"proxy": "http://localhost:3001",
and change your request to:
var request = new Request('http://localhost:3000/user/signin', options);
EDITCleanup for fetch request required for this to work:
fetch('http://localhost:3000/user/signin', { method: 'POST', headers: { 'Content-Type': 'application/json', }, body: JSON.stringify({ 'email': 'admin@domain.com', 'password': '12345' }) // formData}).then(function(response) { ...
Your primary problem is that you aren't setting your request body to be your form data. Instead you are manually encoding your form data as json. Big tipoff: you create the formData
variable but never use it anywhere.
Try this:
options = { headers: headers, mode: 'no-cors', // crossdomain * method: 'post', body: formData};
I've figured it out how to solve the problem, here's the code with the help of this post https://github.com/matthew-andrews/isomorphic-fetch/issues/34.
I hope that helps somebody
Thanks for help me
...// handler recieves the `e` event object formPreventDefault(e) { var headers = new Headers(); headers.append('Content-Type', 'application/json'); var email = document.getElementById('email').value; var password = document.getElementById('password').value; if(email !== '' && password !== '') { var options = { method: 'POST', headers: headers, mode: 'cors', cache: 'default', body: JSON.stringify({ 'email': email, 'password': password }) // formData }; var request = new Request('http://localhost:3001/user/signin', options); fetch(request).then(function(response) { console.log("Success"); return response; }).then(function(json) { console.log(json.errors); }).catch(function(err) { console.log("Error " + err); }) } // prevent submit form e.preventDefault(); }...