仅移动CORS错误:Node.js中的Access-Control-Allow-Origin不允许起源 [英] Mobile only CORS Error: Origin is not allowed by Access-Control-Allow-Origin In Node.js
问题描述
我已经为CORS设置了Node.js(在Google Cloud Platform [GCP]上托管的TypeScript)应用,但是在移动设备上,我仍然收到错误消息(Access-Control-Allow-Origin不允许使用Origin) -desktop可以正常工作。
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错误只有在页面加载几分钟后才会出现在控制台中。 (我删除了有效的URL。)
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无法加载。
"XMLHttpRequest cannot load due to access control checks."
无法加载资源:Access-Control-Allow-Origin不允许起源。
"Failed to load resource: Origin is not allowed by Access-Control-Allow-Origin."
推荐答案
问题DB中缺少数据,因此Node端点未正确返回。错误与问题无关。
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.
这篇关于仅移动CORS错误:Node.js中的Access-Control-Allow-Origin不允许起源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!