Skip to content

CORS blocked fetching

0

Hello,

I have AWS lightsail. I use Sqlite3 to fetch some data from AWS to my frontend (siteground). However, i keep getting the error: Access to fetch at 'https://api.AAA.xyz/api/result?start=AAA&destination=AAA&crypto=AAA' from origin 'https://AAA.xyz' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource. If an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled.

Due to this is cannot fetch the data.

After contacting the siteground customer service i got the following reply: *"The main website AAA.xyz is hosted on the SiteGround IP: AA.AAA.AAA.AAA while api.AAA.xyz is hosted on YY.YYY.YY.YYY

YY.YYY.YY.YYY is with AWS as reported: host YY.YYY.YY.YYY X.AAA.AAA.AAA .in-addr.arpa domain name pointer ec2-YY-YYY-YY-YYY.eu-central-1.compute.amazonaws.com.

Basically the server YY.YYY.YY.YYY for https://api.AAA.xyz is not allowing requests from https://AAA.xyz.

The Access-Control-Allow-Origin header is not present in the server's response. Please log in to your AWS account and edit the setup there."*

However, I already installed: npm install cors I am running my server on pm2. And in my server.js file I have the following code: const https = require('https'); const fs = require('fs'); const express = require('express'); const cors = require('cors'); const sqlite3 = require('sqlite3').verbose(); const app = express(); const port = 3000;

// CORS configuration app.use(cors({ origin: 'https://AAA.xyz', // Allow only your frontend domain methods: ['GET', 'POST', 'OPTIONS'], allowedHeaders: ['Content-Type', 'Authorization'], credentials: true, // Allows including cookies in CORS requests }));

// Handle preflight OPTIONS requests for all routes app.options('*', cors());

Does anybody know which part i am still missing?

Thanking you in advance!

Thijs

asked a year ago291 views
1 Answer
0

The issue is that your AWS server isn’t including the required CORS headers in its responses.

Even though you've set up CORS in your server configuration, you need to ensure that the server on AWS properly returns the Access-Control-Allow-Origin header to allow requests from your frontend domain.

Double-check both your server setup and any AWS configurations to ensure they support CORS requests.

EXPERT
answered a year ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.