ajax跨域请求带cookie

调用网站:a.xxx.com

jQuery(document).ready(function () {
$.ajax({
type: "get",
async: true,
url: "http://www.xxx.com/common/Index", //跨域请求的URL
dataType: "html",
xhrFields: {
withCredentials: true
},
crossDomain: true,

success: function (json) {

$("#header").html(json);
}
});
});

服务端网站:

web.config需要配置

<system.webServer>
<modules>
<remove name="FormsAuthenticationModule" />
</modules>
<httpProtocol>
<customHeaders>
<add name="Access-Control-Allow-Origin" value="http://a.xxx.com" />
<add name="Access-Control-Allow-Credentials" value="true" />
<add name="Access-Control-Allow-Headers" value="Content-Type" />
<add name="Access-Control-Allow-Methods" value="GET,PUT,POST,DELETE,OPTIONS" />
</customHeaders>

</httpProtocol>
</system.webServer>

原文地址:https://www.cnblogs.com/hwisecn/p/4993739.html