Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mobile only CORS Error: Origin is not allowed by Access-Control-Allow-Origin In Node.js

I've set up a Node.js (TypeScript hosted on Google Cloud Platform [GCP]) app for CORS but I'm still getting errors (Origin is not allowed by Access-Control-Allow-Origin) on mobile--desktop works fine.

I've searched but there are so many questions about getting this set up in general, I can't find why it specifically isn't working for mobile.

    this.app.use(cors()); 
    this.app.options('*', cors());

...

    router.use(function(req, res, next) {
          res.header('Content-Type', 'application/json');
          res.setHeader('Access-Control-Allow-Origin', '*');
          res.setHeader('Access-Control-Allow-Methods', 'DELETE,GET,OPTIONS,PATCH,POST');
          res.setHeader('Access-Control-Allow-Headers', 'Origin, Accept, Authorization, Content-Type, X-Requested-With, Access-Control-Allow-Headers, Access-Control-Request-Method, Access-Control-Request-Headers');
          next();
        });

BTW The errors don't appear in the console until a few minutes after the page has loaded. (I've removed the URLs which are valid.)

"XMLHttpRequest cannot load due to access control checks."

"Failed to load resource: Origin is not allowed by Access-Control-Allow-Origin."

like image 311
beachCode Avatar asked Dec 06 '25 04:12

beachCode


1 Answers

The issue was missing data in the DB, so the Node endpoint was not returning properly. The errors had nothing to do with the problem.

like image 90
beachCode Avatar answered Dec 08 '25 17:12

beachCode