http跨域访问

if (Request.Headers.Get("Origin") != null) {
filterContext.HttpContext.Response.AddHeader("Access-Control-Allow-Origin",Request.Headers.Get("Origin"));
filterContext.HttpContext.Response.AddHeader("Access-Control-Allow-Credentials", "true");
}

加入两个header 第一个Origin 表示允许跨域访问的host

第二个设置为ture 表示允许cookie转发

$.ajax({url:'http://localhost:5501/user/login?userinput=13068852262&pwd=xuelei',xhrFields: {
withCredentials: true
},success:function(data){console.log(data)}})

xhrFields的属性withCredentials设置为true 设置cookie转发 

如果不需要转发cookie此项可不设置 header中的第二项也可以不设置 


{readyState: 1, getResponseHeader: ƒ, getAllResponseHeaders: ƒ, setRequestHeader: ƒ, overrideMimeType: ƒ, …}
VM5932:3 {
"state": "success",
"content": "ok",
"total": 0
}

原文地址:https://www.cnblogs.com/ProDoctor/p/7645665.html