美文网首页
接口出现405跨域错误

接口出现405跨域错误

作者: 秒怂的哈士奇爱吃西瓜 | 来源:发表于2021-06-08 17:03 被阅读0次

    web.config 文件

     <httpProtocol>
      <customHeaders>
        <add name="Access-Control-Allow-Origin" value="*" />
        <add name="Access-Control-Allow-Headers" value="Content-Type,Accept,Authdealer, Authstr, Authuser" />
        <add name="Access-Control-Allow-Methods" value="GET, POST, PUT, DELETE, OPTIONS" />
        <!--未实施 HTTP 严格传输安全 (HSTS)-->
        <add name="Strict-Transport-Security" value="max-age=31536000; includeSubDomains" />
        <!--XSS漏洞-->
        <add name="X-Frame-Options" value="SAMEORIGIN" />
        <!--CSP漏洞-->
        <add name="Content-Security-Policy" value="default-src 'self' 'unsafe-inline' 'unsafe-eval' data:;connect-src frame-src 'self'; frame-ancestors 'self' " />
      </customHeaders>
    </httpProtocol>
    

    Global.asax文件
    在用户会话启动后,每次发起的请求都会触发Application_BeginRequest事件,并在请求完成时触发Application_EndRequest事件。

          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("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();
            }
        }
    

    相关文章

      网友评论

          本文标题:接口出现405跨域错误

          本文链接:https://www.haomeiwen.com/subject/okvnsltx.html