Chrome中的慢cors预检选项请求 [英] Slow cors preflight OPTIONS Request in Chrome

查看:443
本文介绍了Chrome中的慢cors预检选项请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

My angular SPA communicates with a node.js-Backend on a different subdomain (api.domain.com). 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预检选项请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆