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

一曲冷凌霜 提交于 2019-12-23 00:45:12

问题


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."


回答1:


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.



来源:https://stackoverflow.com/questions/52413398/mobile-only-cors-error-origin-is-not-allowed-by-access-control-allow-origin-in

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!