GET request to my database gives errors!

Question: I’m trying to make a GET request to https://oolongologiedb.kuramaarts.repl.co/teas/ where the database I want to use is, but I got two errors*****


Repl link: https://replit.com/@Kuramaarts/Oolongologie

*****Errors:

TypeError: Failed to fetchhttps://f6b09c96-90bd-4c63-8215-e58360146bcd.id.repl.co/__replco/static/devtools/devtools.js:56
at window.fetch (https://f6b09c96-90bd-4c63-8215-e58360146bcd.id.repl.co/__replco/static/devtools/devtools.js:56:8647)
at https://f6b09c96-90bd-4c63-8215-e58360146bcd.id.repl.co/script.js:3:1
TypeError: Failed to fetchhttps://f6b09c96-90bd-4c63-8215-e58360146bcd.id.repl.co/__replco/static/devtools/devtools.js:56
at window.fetch (https://f6b09c96-90bd-4c63-8215-e58360146bcd.id.repl.co/__replco/static/devtools/devtools.js:56:8647)
at https://f6b09c96-90bd-4c63-8215-e58360146bcd.id.repl.co/script.js:3:1

Welcome to Replit Ask! Are you returning CORS headers on your database endpoint?

Thanks! Hmmm I’m pretty sure I didn’t, are those useful to make GET requests? I can make the request with Postman and it works fine :thinking:

If you don’t set CORS headers, you can’t make cross-origin requests to the endpoint.

5 Likes

Ohh makes sense… How would I go about modifying my endpoints to implement CORS headers?

It depends on the language and webserver. What are you using for the database repl?

1 Like

Node.js, I have my endpoints code in an index.js file

1 Like

Update: I fixed it!! @9pfs1

I put

app.use((req, res, next) => {
  res.setHeader('Access-Control-Allow-Origin', '*');
  next();
});

// Set the Access-Control-Allow-Methods header to allow GET requests
app.use((req, res, next) => {
  res.setHeader('Access-Control-Allow-Methods', 'GET');
  next();
});

// Set the Access-Control-Allow-Headers header to allow the Content-Type header
app.use((req, res, next) => {
  res.setHeader('Access-Control-Allow-Headers', 'Content-Type');
  next();
});

That fixed it, thank you so much!

2 Likes

You could put that all in one…

app.use((req, res, next) => {
  res.setHeader('Access-Control-Allow-Origin', '*');
  res.setHeader('Access-Control-Allow-Methods', 'GET');
  res.setHeader('Access-Control-Allow-Headers', 'Content-Type');
  next();
});
4 Likes

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.