Reputation: 155
I am trying to make a login for a website. I am trying to send a http request with axios, to save the inputted data by the user, name, password etc and save it to a database. When I make the request with postman it works but when I try to make the request on the client side it won't. I am getting this error from the console:
Error:
("Network Error"
createError createError.js:17
handleError xhr.js:80
index.js:1375
e index.js:1375
onSubmit Register.js:39)
I have tried various things and tried to scrape the web for the answer, but I cant find anything.If you could give me any help that would be great. I have tried using local host 3000 and now the local. Thanks!!!!!
Heres the code
import React, { useState } from 'react';
import '../../App';
import axios from 'axios';
const Register = () => {
const [formData, gatherData] = useState({
name: '',
email: '',
password: '',
paswrd: ''
});
const { name, email, password, paswrd } = formData;
//on submit checks if passwords are valid, if they are valid create a new instance of user
const onSubmit = async element => {
element.preventDefault();
if (password !== paswrd) {
console.log("Passwords are not matching ")
}
else {
const newUser = {
name,
email,
password
}
try {
const configuration = {
headers: {
'Content-Type': 'application/json'
}
}
const body = JSON.stringify(newUser);
const res = await axios.post('http://localhost:5000/apis/user', body, configuration);
console.log(res.data);
} catch (err) {
console.error(err)
}
}
}
export default Register;
Upvotes: 0
Views: 72
Reputation: 104
Try to add cors on your server
const cors = require('cors');
app.use(cors());
Upvotes: 1