隐藏

IIS配置允许跨域后,还是无法跨域,请检查<handlers>

发布:2021/9/26 16:13:37作者:管理员 来源:本站 浏览次数:970

IIS配置允许跨域后,还是无法跨域,提示 index.html:1 Access to XMLHttpRequest at '        XXXX' from origin 'http://127.0.0.1:8080' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource.请检查

<system.webServer>
    <httpProtocol>
      <!--跨域配置开始-->
      <customHeaders>
        <add name="Access-Control-Allow-Origin" value="*" />
        <!--支持的http 动作-->
        <add name="Access-Control-Allow-Headers" value="AuthToken, Authorization, Origin, Content-Type, Accept, X-Requested-With" />
        <!--响应头 请按照自己需求添加 这里新加了token这个headers-->
        <add name="Access-Control-Request-Methods" value="GET, POST, PUT, DELETE, OPTIONS" />
        <!--允许请求的http 动作-->
      </customHeaders>
      <!--跨域配置结束-->
    </httpProtocol>
    
    <handlers>
      <remove name="ExtensionlessUrlHandler-Integrated-4.0"/>
      <remove name="OPTIONSVerbHandler"/>
      <remove name="TRACEVerbHandler"/>
      <add name="ExtensionlessUrlHandler-Integrated-4.0" path="*." verb="*" type="System.Web.Handlers.TransferRequestHandler"
        preCondition="integratedMode,runtimeVersionv4.0"/>
    </handlers>
 
   
  </system.webServer>


关键问题是这两行代码

    <remove name="OPTIONSVerbHandler"/> <!--选择动词处理程序-->
    <remove name="TRACEVerbHandler"/>  <!--跟踪动词处理程序-->

方式一:直接去掉这两行代码

      <system.webServer>
        <httpProtocol>
          <!--跨域配置开始-->
          <customHeaders>
            <add name="Access-Control-Allow-Origin" value="*" />
            <!--支持的http 动作-->
            <add name="Access-Control-Allow-Headers" value="AuthToken, Authorization, Origin, Content-Type, Accept, X-Requested-With" />
            <!--响应头 请按照自己需求添加 这里新加了token这个headers-->
            <add name="Access-Control-Request-Methods" value="GET, POST, PUT, DELETE, OPTIONS" />
            <!--允许请求的http 动作-->
          </customHeaders>
          <!--跨域配置结束-->
        </httpProtocol>
        
        <handlers>
          <remove name="ExtensionlessUrlHandler-Integrated-4.0"/>
          <add name="ExtensionlessUrlHandler-Integrated-4.0" path="*." verb="*" type="System.Web.Handlers.TransferRequestHandler"
            preCondition="integratedMode,runtimeVersionv4.0"/>
        </handlers>
     
       
      </system.webServer>

方法二:直接取消 handlers

    <system.webServer>
        <httpProtocol>
          <!--跨域配置开始-->
          <customHeaders>
            <add name="Access-Control-Allow-Origin" value="*" />
            <!--支持的http 动作-->
            <add name="Access-Control-Allow-Headers" value="AuthToken, Authorization, Origin, Content-Type, Accept, X-Requested-With" />
            <!--响应头 请按照自己需求添加 这里新加了token这个headers-->
            <add name="Access-Control-Request-Methods" value="GET, POST, PUT, DELETE, OPTIONS" />
            <!--允许请求的http 动作-->
          </customHeaders>
          <!--跨域配置结束-->
        </httpProtocol>
        
        <!--
    <handlers>
          <remove name="ExtensionlessUrlHandler-Integrated-4.0"/>
          <add name="ExtensionlessUrlHandler-Integrated-4.0" path="*." verb="*" type="System.Web.Handlers.TransferRequestHandler"
            preCondition="integratedMode,runtimeVersionv4.0"/>
        </handlers>
    -->
     
       
      </system.webServer>

其他
IIS的处理程序映射Mapping配置文件路径

C:\Windows\System32\inetsrv\config\applicationHost.config
参考的官方文档地址:

https://docs.microsoft.com/en-us/iis/configuration/system.webserver/handlers/add