zoukankan      html  css  js  c++  java
  • webapi处理OPTIONS请求

    报错1信息

     Access to XMLHttpRequest at 'http://localhost:4445/api/v/getmsg' from origin 'http://localhost:9528' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: The value of the 'Access-Control-Allow-Origin' header in the response must not be the wildcard '*' when the request's credentials mode is 'include'. The credentials mode of requests initiated by the XMLHttpRequest is controlled by the withCredentials attribute.

    解决方案

    参考资料:https://segmentfault.com/q/1010000016765176把value值改成特定的域名。

      <system.webServer>   
        <httpProtocol>
          <customHeaders>
            <add name="Access-Control-Allow-Origin" value="http://localhost:9528" />
        </httpProtocol>    
      </system.webServer>

    报错2信息 

    Access to XMLHttpRequest at 'http://localhost:4445/api/v/getmsg' from origin 'http://localhost:9528' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: The value of the 'Access-Control-Allow-Credentials' header in the response is '' which must be 'true' when the request's credentials mode is 'include'. The credentials mode of requests initiated by the XMLHttpRequest is controlled by the withCredentials attribute.

    解决方案

    增加一行配置文件

    <add name="Access-Control-Allow-Credentials" value="true" />

    报错3信息

    Access to XMLHttpRequest at 'http://localhost:4445/api/v/getmsg' from origin 'http://localhost:9528' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: It does not have HTTP ok status.

    原因

    浏览器请求接口时会发送两个请求,一个是预请求,相当于确认请求,第二个请求才是你要发送的真正的请求,而这个错误信息说明的是第一个OPTINOS请求失败,在服务端没有处理这个method为OPTIONS的请求,需要对它处理一下:

    解决方案:

    参考资料:关于Web API 2.0中的Options请求返回405的问题

                      HTTP Module

        public class SpecialMethodModule : IHttpModule
        {
            public SpecialMethodModule()
            {
            }
    
            public void Init(HttpApplication app)
            {
                app.BeginRequest += new EventHandler(this.BeginRequest);
            }
    
            public void Dispose()
            {
            }
    
            public void BeginRequest(object resource, EventArgs e)
            {
                HttpApplication app = resource as HttpApplication;
                HttpContext context = app.Context;
                if (context.Request.HttpMethod.ToUpper() == "OPTIONS")
                {
                    context.Response.StatusCode = 200;
                    context.Response.End();
                }
            }
        }

    在web.config中增加module节点,参考微软官方文档,根据IIS版本不同,增加的节点方式也不同,我是IIS10.0

    <configuration>
      <system.webServer>
        <modules>
          <add name="HelloWorldModule" type="HelloWorldModule"/>
        </modules>
      </system.webServer>
    </configuration>

    这是我遇到的问题,进行解决以及整理,有其他问题欢迎沟通。

  • 相关阅读:
    python 输入和输出
    python 深入模块和包
    python 模块
    python 字典 注意点
    javaNIO核心概念
    redis使用bit做只有两种情况的“状态“统计(如是否在线的用户统计)
    mysqlbinlog二三事儿
    mysql在windows下的服务安装
    javassist标识符
    使用redis调用lua脚本的方式对接口进行限流
  • 原文地址:https://www.cnblogs.com/dawenyang/p/10956521.html
Copyright © 2011-2022 走看看