asp.net中的jquery设置会话

我正在尝试在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 // *************************************  }, onDelete: function (item) { }, theme: "facebook" }); }); 

asp.net中的jquery设置会话

无法使用jQuery / javascript在客户端设置会话。 会话在服务器端维护,必须在服务器端而不是在jQuery中设置,尽管你可以在jQuery中使用sessoin值。 您可以向服务器发送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/