解决Jquery跨域请求Webapi时出现每次请求的sessionID都是不一样的
当webapi开启session时,出现同一请求域,多次请求时SessionID是变化的情况,即每次请求出现一个新的SESSIONID, 导致后台webapi无法判断且记住当前请求的用户,如下图:



最后发现一个原因即可解决,就是在ajax请求时加入以下两行代码:
,xhrFields: {
withCredentials: true
}
,crossDomain: true完整的ajax请求如下:
$.ajax({
url: ' ValidateCode?t=' + new Date().getMilliseconds()
, type: 'get'
, dataType: 'json'
, data: null
, headers: {
"Content-Type": "application/json"
}
//开始 :以下两句必须加上,否则每次请求的Sessionid都不一样
,xhrFields: {
withCredentials: true
}
,crossDomain: true
//结束
, success: function (res) {
$('#img').attr('src', res.data);
//layer.msg(res.msg);
layer.close(layerindex);
}
, error: function (XMLHttpRequest, textStatus, errorThrown) {
layer.close(layerindex);
//// 状态码
//console.log(XMLHttpRequest.status);
//// 状态
//console.log(XMLHttpRequest.readyState);
//// 错误信息
//console.log(textStatus);
}
});
















