ANGULAR C#-CORS策略:对预飞行请求的响应未通过访问控制检查:请求的资源上没有“Access-Control-Allow-Origin”标头。
这对于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();
}
}