ITPub博客

首页 > 应用开发 > Java > ajaxpro的ajax函数内不能使用session

ajaxpro的ajax函数内不能使用session

原创 Java 作者:jyb2014 时间:2014-09-30 13:28:33 0 删除 编辑
ajaxpro的ajax函数内不能使用session?比如还有cache,viewstate,response,request,一使用到这些 ,程序马上就断
开了。还有哪些操作不能使用,为什么?
解决方案:
添加一个 AjaxPro.HttpSessionStateRequirement 枚举的值到你的 AjaxPro.AjaxMethodAttribute中.
例如:
[AjaxPro.AjaxMethod(AjaxPro.HttpSessionStateRequirement.Read)]    
现在你就可以像下面这样存取Session的值了: this.Session["0"].ToString().
另外一个问题:如何在AjaxMethod中使用Cookie?
答案是不能使用"this"的引用存取cookie ,你必须使用HttpContext.Current
例如:
HttpContext.Current.Request.Cookies[Name].Value.
在服务器端page_load
AjaxPro.Utility.RegisterTypeForAjax(typeof(test));
this.Button_Write.Attributes.Add("onclick","WriteSession();");//写session
this.Button_Read.Attributes.Add("onclick", "ReadSession();");//读session
其他写和读的方法
///
/// 写session
///

///
[AjaxPro.AjaxMethod(AjaxPro.HttpSessionStateRequirement.ReadWrite)]
public void WriteSession(string str)
{
Session["UserName"] = str;
}
///
/// 读session
///

///
[AjaxPro.AjaxMethod(AjaxPro.HttpSessionStateRequirement.ReadWrite)]
public string ReadSession()
{
string str = "";
if (Session["UserName"] != null)
{
str = Session["UserName"].ToString();
}
return str;
}
客户端代码:
//4、访问Session的值
//写入
function WriteSession()
{
var str = "HAHA";
test.WriteSession(str,CallBack_WriteSession);
}
function CallBack_WriteSession(res)
{
if(res.error == null)
{
alert("OK");
}
else
{
alert(res.error.message);
}
}
//访问
function ReadSession()
{
test.ReadSession(CallBack_ReadSession);
}
function CallBack_ReadSession(res)
{
if(res.error == null)
{
alert(res.value);
}
else
{
alert(res.error.message);
}
}

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/29753604/viewspace-1285832/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2014-07-15

  • 博文量
    101
  • 访问量
    196522