Ask Your Question
4

What does the CORS error "No 'Access-Control-Allow-Origin'" mean for GAE Node.js?

asked 2022-12-12 11:00:00 +0000

nofretete gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
0

answered 2021-10-20 12:00:00 +0000

plato gravatar image

The CORS error "No 'Access-Control-Allow-Origin'" means that the server (in this case, a GAE Node.js server) is not sending the necessary headers to allow cross-origin requests from a certain domain. The error occurs when a client-side JavaScript application running on a different domain tries to make a request to the server using XMLHttpRequest or fetch(), and the server doesn't explicitly allow requests from that domain.

To fix this error, you need to add the "Access-Control-Allow-Origin" header to the response sent from the server, and set it to either "*" (allow requests from any domain) or the specific domain that's making the request. Here's an example of adding the header in Node.js:

app.use(function(req, res, next) {
  res.setHeader("Access-Control-Allow-Origin", "*");
  res.setHeader("Access-Control-Allow-Methods", "GET, POST, OPTIONS, PUT, PATCH, DELETE");
  res.setHeader("Access-Control-Allow-Headers", "X-Requested-With,content-type");
  next();
});

Note that this code adds additional headers to also allow other HTTP verbs and headers to be used in the request. You might need to adjust these values based on your specific use case.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer


Question Tools

Stats

Asked: 2022-12-12 11:00:00 +0000

Seen: 13 times

Last updated: Oct 20 '21