我使用ajax来调用远程服务器中的API。
function LoginUser() {
$.ajax({
type: "POST",
url: "http://myServerName/MyAPI" + "/LoginUser",
data: "{userName: 'aaa'}"
contentType: "application/json; charset=utf-8",
dataType: "json",
crossDomain: true,
success: function (response) {
if (response.d != "") {
window.location = "WelcomePage.aspx";
}
else if (response.d == "") {
}
},
error: function (response) {
}
});
return false;
}
API:
[WebMethod(EnableSession=true) ]
public static string LoginUser(string userName)
{
MyServiceClient client = new MyServiceClient();
string userID = client.LoginUser(userName).ToString();
HttpContext.Current.Session.Add("userID", userID);
return userID;
}
response.d返回userId,但是当我尝试获取会话时,在welcomePage中我得到null
string userId = Session["userID"].toString()
如果我在登录和欢迎页面的相同解决方案中使用API页面,它可以完美地运行。
感谢您的帮助