how do i set the value on a session with jquery?

白昼怎懂夜的黑 提交于 2019-12-13 09:35:50

问题


I need to set the value of a session with jquery. im thinking i need it to be done on server side but how do i do that with jquery? i got a link thats gone trigger the whole thing and my code so far is this.

<a href="#" id="showCart">Show Cart</a>


$('#showCart').click(function() {
            $('#cartContainer').show();
            //need to the set a session for the site to know if carts gone be visible
});

回答1:


For invoking something in the server side, you shall have to invoke a web method or a web page (aspx) from your jQuery method. You may have to pass on the required data as parameter(s). The task of setting the value to the session will be done from the server side code.




回答2:


You'll need to create a handler on the server that sets the session - in ASP.NET MVC, you simply create an action method that takes whatever data you need to set the session variable, and sets it. You then call the action method via jQuery's AJAX API.

A better option though, is to set a cookie instead. That will be available on both sides, and can be set directly on the client. Read more...




回答3:


This is what i did

$('#showCart').click(function() {
            $('#cartContainer').show();
            $.post('/Home/SaveSession');
});



    [WebMethod]
    public void SaveSession()
    {
        Session["IsCartVisible"] = "true";
    }


来源:https://stackoverflow.com/questions/6729538/how-do-i-set-the-value-on-a-session-with-jquery

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!