在本地使用Google Chrome浏览器禁用OPTIONS请求

Disable OPTIONS requests on localhost with Google Chrome browser?

本文关键字:浏览器 OPTIONS 请求 Chrome Google      更新时间:2023-09-26

我正在做一个项目,使用两个不同的集合,前端和后端。

每次刷新页面时,它都会调用后端(localhost)来加载数据,但每次都要重做OPTIONS请求以确保CORS设置正确。

因为它是本地主机,我想知道是否有可能告诉Chrome"别担心,我知道这个来源,你不必检查它",或者只是"检查它,但快速",因为每次(并且出于我无法确定的原因),一个选项请求大约需要13(!!!!)秒,每次我点击刷新,这是非常烦人的。

我试过--disable-web-security,但这并没有改变任何东西

我们通常在全局变量中这样做。如果我们有一个。net应用程序在全球。在ax文件中,我们获取选项头并将其替换为合适的头允许CORS支持。

所以在你的情况下,支持必须从API(服务端)端提供。

protected void Application_BeginRequest(object sender, EventArgs e)
        {
            HttpContext.Current.Response.AddHeader("Access-Control-Allow-Origin",
 "*");
            if (HttpContext.Current.Request.HttpMethod == "OPTIONS")
            {
                HttpContext.Current.Response.AddHeader("Cache-Control",
 "no-cache");
                HttpContext.Current.Response.AddHeader(
"Access-Control-Allow-Methods",
 "GET, POST");
                HttpContext.Current.Response.AddHeader(
"Access-Control-Allow-Headers",
 "Content-Type, Accept");
                HttpContext.Current.Response.AddHeader("Access-Control-Max-Age", 
"1728000");
                HttpContext.Current.Response.End();
            }
        }