Asp.net Mvc、webApi配置允许跨域
Web.config 下<system.webServer> 节点下配置
<httpProtocol> <customHeaders> <add name="Access-Control-Allow-Origin" value="http://localhost:60699" /> <add name="Access-Control-Allow-Headers" value="*" /> <add name="Access-Control-Allow-Methods" value="GET, POST, PUT, DELETE" /> </customHeaders> </httpProtocol>
设置允许跨域传递Cookie
<system.webServer> <httpProtocol> <customHeaders> <add name="Access-Control-Allow-Origin" value="http://localhost:8080" /> <add name="Access-Control-Allow-Headers" value="content-type" /> <add name="Access-Control-Allow-Methods" value="*" /> <add name="Access-Control-Allow-Credentials" value="true"/> </customHeaders> </httpProtocol> </system.webServer>
前端 请求的时候需设定:withCredentials = true; 设置了widthCredentials为true的请求中会包含远程域的所有cookie