[英]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"
});
});
這是一些鏈接
在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.