我在.NET 4上有一个asp.NET WCF.该服务用于验证用户.我们正在提交用户名和密码,然后应返回包含身份验证cookie的HTTP标头.使用本地托管的测试页面,这是正常的.我现在正在访问头域信息跨域.我已将测试页安装在另一台计算机上,并配置为调用WCF.呼叫正常,呼叫中的"数据"回复正确.但是,我无法使用以下任一方法访问标头信息:
alert(xmlHttp.getAllResponseHeaders());
Run Code Online (Sandbox Code Playgroud)
要么
alert(xmlHttp.getResponseHeader("Set-Cookie"));
Run Code Online (Sandbox Code Playgroud)
使用IE中的调试器和Firefox的"Live HTTP Header"插件,我可以看到正在返回标头信息.
在我的全局ajax页面中,我设置了处理CORS的响应.
private void EnableCrossDomainAjaxCall()
{
HttpContext.Current.Response.AddHeader("Access-Control-Allow-Origin", "*");
if (HttpContext.Current.Request.HttpMethod == "OPTIONS")
{
HttpContext.Current.Response.AddHeader("Cache-Control", "no-cache");
HttpContext.Current.Response.AddHeader("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE");
HttpContext.Current.Response.AddHeader("Access-Control-Allow-Headers", "Content-Type, Accept");
HttpContext.Current.Response.AddHeader("Access-Control-Max-Age", "1728000");
HttpContext.Current.Response.End();
}
}
Run Code Online (Sandbox Code Playgroud)
这是我用来调用服务的AJAX:
$("#btnLogin").click(function(e) {
var geturl;
geturl = $.ajax({
// type: "POST",
type: "GET",
contentType: "application/json; charset=utf-8",
url: 'http://10.0.4.66/AuthenticationService.svc/Login?Name=test&password=pwsd',
// url: '../SecurityServer/AuthenticationService.svc/Login?Name=test&password=pwsd',
dataType: "jsonp",
error: function(request, status, error) {
alert('Error Occured');
},
crossdomain: true,
success: function(data, textStatus, xmlHttp) {
// alert(xmlHttp.getResponseHeader("Content-Type"));
document.write(xmlHttp.getResponseHeader("Content-Type") + "<br/>");
alert(xmlHttp.getAllResponseHeaders()); …Run Code Online (Sandbox Code Playgroud)