首页 新闻 赞助 找找看

asp.net中用JS如何清除Session对象

0
[已解决问题] 解决于 2010-05-04 13:36

我的需求现在是这样的,要求用JS脚本清除Session对象,网上找了很多,都说是客户端不能操作服务端对象,请各位园子里的大牛指点一二,多谢了。

Joe_true的主页 Joe_true | 初学一级 | 园豆:98
提问于:2010-04-26 12:46
< >
分享
最佳答案
0

可以使用JS调用服务器处理文件来实现清空Session对象。

HTML:
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>无标题文档</title>
<script type="text/javascript">
var xmlhttp;
function createXmlhttp(){
    if(window.XMLHttpRequest){
        xmlhttp = new XMLHttpRequest();
            }
            else if(window.ActiveXObject){
            try{
            xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
            }catch(e){
            xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
            }
        }
    return xmlhttp;

}
function ClearSession()
{
    createXmlhttp();
    var url="Service.asmx/ClearSession";
    xmlhttp.open("POST",url,true);
    xmlhttp.onreadystatechange=handleStateChange;
    xmlhttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded;");
    xmlhttp.send(queryString);
}
function handleStateChange()
{
    if(xmlhttp.readyState==4)
    {
        if(xmlhttp.status==200)
        {
            //清空成功
        }
    }
}
</script>
</head>
<body>
<input name="Submit" type="button" onclick="ClearSession();" value="清空Session" />
</body>

WEB服务:
<%@ WebService Language="C#" class="Service" %>
using System;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;

using System.Web.SessionState;//不引入是不可以操作Session的

[WebService(Namespace = "http://tsingjun.cn/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
public class Service : System.Web.Services.WebService
{
  [WebMethod(true)]
  public int ClearSession()
  {

  //清空Session代码
    return 0;
  }
}


Astar | 高人七级 |园豆:40805 | 2010-04-26 13:23
其他回答(3)
0

可以考虑用ajax的,在后台代码中清除Session对象的。

陈 锋 | 园豆:210 (菜鸟二级) | 2010-04-26 13:01
0

1、.ashx 文件处理。但是记得要加一个session状态的引用。

2、用ajax中的 PageMethes.方法名调用后台方法。用到ScriptManager。

邢少 | 园豆:10926 (专家六级) | 2010-04-26 14:27
0

其实有个简单的就是清除客户端aspnetcookieid这个cookie就行了,不过服务端还是占用内存了

如果要session在服务端占用空间的话就用上面的ajax了

LittlePeng | 园豆:3445 (老鸟四级) | 2010-04-26 17:02
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册