ajax跨域请求带cookie

Posted on 2015-11-25 09:51  hwisecn  阅读(719)  评论(1编辑  收藏  举报

调用网站: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>

Copyright © 2024 hwisecn
Powered by .NET 9.0 on Kubernetes