Pregunta

I'm working on a project that use two distinct set, the frontend and the backend.

Everytime I refresh the page, it calls the backend (localhost too) to load the data, but every time, it redo an OPTIONS request to make sur the CORS are set correctly.

Since it's localhost, I'd like to know if there is some possibility to tell Chrome "Don't worry, I know this source, you don't have to check it", or just "Check it, but fast" because everytime (and for a reason I can't determine), an OPTIONS request takes about 13 (!!!!) seconds, everytime I hit refresh, and it's very annoying.

I tried to --disable-web-security, but this doesn't change anything.

¿Fue útil?

Solución

We generally do this at inside our global.asax file if we are having a .Net app In the global.asax file we grab the options header and replace it with suitable headers to allow the CORS support.

So in your case too, the support has to be given from the API(Service end) end.

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();
            }
        }
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top