Categories
JavaScript Answers

How to fix ‘Access-Control-Allow-Origin’ issue when API call made from React isomorphic app?

Spread the love

To fix ‘Access-Control-Allow-Origin’ issue when API call made from React isomorphic app, we enable CORS on the back end.

For instance, we write

app.use((req, res, next) => {
  res.header("Access-Control-Allow-Origin", "*");
  res.header("Access-Control-Allow-Headers", "X-Requested-With");
  next();
});

to enable CORS in a middleware by sending the Access-Control-Allow-Origin and Access-Control-Allow-Headers response headers.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published.