Reputation: 389
I've been developing a React app to authenticate an user with express backend. I've tested out the express server by sending a POST request using postman and everything works fine.
But I'm getting a weird error while doing the same POST request using the React App.
I checked out the request payload and the JSON data sent is correct.
Given below is the code snippet for axios action.
export const registerUser=(userData, history) => dispatch => {
axios.post("http://localhost:5000/users/register",userData)
.then( res => {
console.log(res)
history.push("/login") } )
}
Any pointers on how to solve this issue is much appreciated.
Upvotes: 0
Views: 79
Reputation: 56
On the backend you need to set cors, first add the cors package and use it like
const app = express();
app.use(cors());
this will allow all site to connect to your backend, you can restrict it so that only some site can use it like
app.use(
cors({
origin: "your app route",
})
);
Upvotes: 2