vue项目中已拦截跨源请求:同源策略禁止读取位于....(原因:CORS 请求未能成功)解决方案

时间:2025-01-21 10:38:51

手动给配置,添加如下方法

  protected void Application_BeginRequest(object sender, EventArgs e)
        {
             app = sender as ;
            HttpResponse response = ;
            //支持跨域        
            if ( == "OPTIONS")
            {
                //支持跨域
                ("Access-Control-Allow-Methods", "*");
                ("Access-Control-Allow-Headers", "*");
                ("Access-Control-Allow-Origin", "*");
                ("Cache-Control", "no-cache");  
                 = 200;
                ();
            }
            else
            {
                ("Access-Control-Allow-Origin", "*");
                ("Access-Control-Allow-Headers", "*");
                ("Access-Control-Allow-Methods", "*");
                ("Cache-Control", "no-cache");
                //("Access-Control-Allow-Methods", "GET, POST,PUT, DELETE");
                //("Access-Control-Allow-Headers", "Content-Type");
            }
        }

最后记得退出所有终端,重新进入终端即可解决跨域报错问题。