1、跨域请求中默认不带cookie等验证凭证html
尤为对于post请求。ajax
对于ajax请求,其中post,get均可以正常访问。api
withCredentials: false, // 容许携带cookie
若是设置容许带cookie那么会遇到一个错误:跨域
Failed to load http://pre.api.jmxy.mockuai.c...:
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'.
Origin 'http://pre.promotion.jmxy.moc...' is therefore not allowed access.
The credentials mode of requests initiated by the XMLHttpRequest is controlled by the withCredentials attribute.
这个错误的意思:cookie
也就是说Access-Control-Allow-Credentials
设置为true
的状况下Access-Control-Allow-Origin
不能设置为 *
post
解决方案:ui
后台响应头中设置对应的容许的域名。url
2、Asp.Net Core中跨域处理+附带Cookie验证spa
注:登陆后cookie存储,由客户端完成,后台仅验证有效性。.net
1.请求中指定
withCredentials:true //支持附带详细信息
$.ajax({ url: apiUrl.getCookie('getone'), data: { age: 11 }, xhrFields: { withCredentials:true //支持附带详细信息 }, crossDomain:true,//请求偏向外域 success: function (data) { alert(data); } });
2.响应中,单独设置容许的域名
//设置跨域访问 services.AddCors(options => { options.AddPolicy("any", builder => { builder.WithOrigins("http://www.gongjuji.net/", "http://localhost:8080", "http://localhost:8081", "http://localhost:8082") .AllowAnyMethod() .AllowAnyHeader() .AllowCredentials(); }); });
3、特别说明
1.当前设置仅针对同一个根域名的状况下,好比:www.gongjuji.net 和 erp.gongjuji.net 这样。
2.
更多:
Asp.Net WebApi 启用CORS跨域访问指定多个域名