提问者:小点点

ANGULAR C#对preflight请求的响应没有通过访问控制检查请求的资源上没有Access-Control-Allow-Origin标头


ANGULAR C#-CORS策略:对预飞行请求的响应未通过访问控制检查:请求的资源上没有“Access-Control-Allow-Origin”标头。


共1个答案

匿名用户

这对于GET和POST很好用。 (所有方法)

将其添加到global.asax/global.asax.cs文件中。

protected void Application_BeginRequest(object sender, EventArgs e)
    {
        // Preflight request comes with HttpMethod OPTIONS
        // The following line solves the error message
        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");
            // If any http headers are shown in preflight error in browser console add them below
            HttpContext.Current.Response.AddHeader("Access-Control-Allow-Headers", "Content-Type, Accept, Pragma, Cache-Control, Authorization ");
            HttpContext.Current.Response.AddHeader("Access-Control-Max-Age", "1728000");
            HttpContext.Current.Response.End();
        }
    }