Chrome中的慢cors预检选项请求

编程入门 行业动态 更新时间:2024-10-27 20:24:43
本文介绍了Chrome中的慢cors预检选项请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在努力解决一个怪异的问题,只在Chrome中发生。

I'm struggling with a freaky problem, that only occurs in Chrome.

我的角度SPA与另一个子域(api.domain )。为使此工作正常进行,我使用了 cors npm软件包。一切正常,除了OPTIONS预检在Chrome中的下载时间。

My angular SPA communicates with a node.js-Backend on a different subdomain (api.domain). To get this working, i use the cors npm package. Everything works fine, except the download-time of OPTIONS preflight in Chrome.

选项的屏幕截图比PUT慢 时间标签显示大多数时间都花在下载内容上,但我的回复中没有Content-Body。

此时间仅在Chrome中发生。 Firefox,IE,Edge和Opera不受影响。

This timing only occurs in Chrome. Firefox, IE, Edge and Opera are not affected.

我在docker容器中使用具有express和cors的node.js。前端是一个有角度的1 SPA。

I use node.js with express and cors inside docker containers. Frontend is an angular 1 SPA.

有人知道如何解决这个问题吗?

Has anyone an idea how to fix this?

推荐答案

<如果使用的是Express,可以尝试使用此技巧,并使用所有CORS内容编写中间件功能,检查请求是否为预检,然后返回200。希望此方法可以为您解决。但是再说一次,我不使用这种依赖关系,而且很奇怪,它只在chrome中出现。

If you are using express you can try this hack and write a middleware function with all your CORS stuff, check if the request is a preflight and just return 200. Hope this fixes it for you. But then again I don't use this dependency and its weird that it only occurs in chrome.

app.use(function(req, res, next) { //CORS res.setHeader('Access-Control-Allow-Origin', '*'); res.setHeader('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE'); res.setHeader("Access-Control-Allow-Headers", "Origin, x-access-token, X-Requested-With, Content-Type, Accept"); if ('OPTIONS' == req.method) { res.send(200); } else { next(); } });

更多推荐

Chrome中的慢cors预检选项请求

本文发布于:2023-10-09 16:40:56,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1476226.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:选项   Chrome   cors

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!