繁体   English   中英

jQuery在asp.net中设置会话

[英]jquery setting session in asp.net

我正在尝试在jquery中设置会话。 这是代码,但无法弄清楚。 请查看评论下方的行。 我该怎么做?

$(document).ready(function () {
    $("#demo-input-facebook-theme").tokenInput("http://xxx.com/MedService.aspx", {
        onAdd: function (item) {
            // *************************************
            // I want to add item.name to below code
            // *************************************
            <% HttpContext.Current.Session["Session_Post_Kategori"] += "item.name"; %>
        },
        onDelete: function (item) { },
        theme: "facebook"
     });
});

jQuery在asp.net中设置会话

无法使用jQuery / javascript在客户端设置会话。 尽管可以在jQuery中使用sessoin值,但会话是在服务器端维护的,并且必须在服务器端而不是jQuery中设置。 您可以将ajax调用发送到服务器,以通过javascript设置会话。

会话存储在服务器端...,而Javascript / Jquery是客户端脚本..因此,您无法从Javascript访问会话...但是,可以使用ajax将值发布到服务器并将其存储在服务器中。

例..

 onAdd: function (item) {
     $.post(yoururl,{data:item.name});//<--- this will post the data to your url (controller) and you can set the session there

无法设置会话客户端,因为<%%>将在页面加载时执行,并将与其余HTML一起呈现。 但是,您可以使用它来读取值并根据它执行一些操作。 您可能想尝试@Adil所说的。

我希望这可以使一切都变得清晰。

您无法像尝试一样进行设置。

您可以执行的操作可以创建generic http handler
从jQuery调用它
在该处理程序中设置会话值。

public class AddSalesLead : IHttpHandler, IRequiresSessionState
{
    public void ProcessRequest(HttpContext context)
    {
        context.Response.Session= context.Request["yourvalue"];
    }
    public bool IsReusable
    {
        get{return false;}
    }
}

并从jquery调用它

$(document).ready(function () {
    $("#demo-input-facebook-theme").tokenInput("http://xxx.com/MedService.aspx", {
        onAdd: function (item) {
          //call the handler here to set session value
          $.ajax({
               type: "POST",
               url: "yourhandler.ashx?yourvalue="+"value",
               success: function (data) {
               },
               error: function () {
               },
                async: true
           });
        },
        onDelete: function (item) { },
        theme: "facebook"
     });
});

编辑1

这是一些链接
在jquery.click()中设置ASP.NET会话
http://brijbhushan.net/2011/05/29/call-httphandler-from-jquery-pass-data-and-retrieve-in-json-format/

我找到此网页,对于解决此问题确实很有帮助。 它工作完美,非常干净。 它可以帮助避免传递会话或cookie。

http://codewala.net/2011/05/29/call-httphandler-from-jquery-pass-data-and-retrieve-in-json-format/

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM