调用页面方法时未保留会话状态
我的服务器端代码:
[WebMethod(CacheDuration = 0, EnableSession = true)]
public static int UserID()
{
if (HttpContext.Current.Session["UserID"] == null) return 0;
int UserID = Convert.ToInt32(HttpContext.Current.Session["UserID"]);
return (UserID);
}
我的客户端代码:
$.ajax({
type: "POST", cache: false,
url: "Login.aspx/UserID",
data: "{'r':" + rnd() + "}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function(msg) {
alert(msg);
}
});
该代码在我的本地主机上运行良好。 和“UserID”ajax 调用,返回 Session 参数的正确值。
但是当我尝试在服务器上上传我的网站时,“UserID”ajax 调用总是返回 false!
我的服务器是asp.net 2.0,我使用jquery 1.3.2
所以请帮助解决这个问题。
My server side code:
[WebMethod(CacheDuration = 0, EnableSession = true)]
public static int UserID()
{
if (HttpContext.Current.Session["UserID"] == null) return 0;
int UserID = Convert.ToInt32(HttpContext.Current.Session["UserID"]);
return (UserID);
}
My Client side code:
$.ajax({
type: "POST", cache: false,
url: "Login.aspx/UserID",
data: "{'r':" + rnd() + "}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function(msg) {
alert(msg);
}
});
This codes runs well on my localhost.
and the "UserID" ajax call, return the right value of the Session parameter.
but when i try to upload my website on the server, the "UserID" ajax call always returns false!!
my server is asp.net 2.0 and I'm using jquery 1.3.2
So please help to solve this problem.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
阅读本文:ASP.NET 会话状态常见问题解答
我在这个问题中找到了答案:
A:可能是机器名问题。 请参阅 http://support.microsoft.com/default.aspx ?scid=kb;EN-US;q316112 。
A:可能是因为应用程序回收。 请参阅 http://support.microsoft.com/default.aspx ?scid=kb;en-us;Q316148
将 sessionState 模式更改为“StateServer”解决了该问题。
使用下面的代码:
Read this article: ASP.NET Session State FAQ
I found my answer in this questions:
A: Maybe machine name problem. See http://support.microsoft.com/default.aspx?scid=kb;EN-US;q316112 .
A: Probably because of application recycle. See http://support.microsoft.com/default.aspx?scid=kb;en-us;Q316148
Changing sessionState mode to "StateServer" solved the problem.
Use code below:
有几点需要检查:
您是否使用 WebFarm 配置? 在 WebFarm 配置中使用会话时可能会出现问题。
您是否使用 Fiddler 或 FireBug 等工具检查过网络流量? AJAX 调用期间是否发送 ASP.NET Auth cookie?
您能确认您的会话变量有效吗?
A few things to check :
Are you using WebFarm configuration? there might be a problem when using session in WebFarm configuration.
Have you check your network traffic using tools like Fiddler or FireBug? Is ASP.NET Auth cookie sent during AJAX call?
Can you confirm your session var is valid?