[英]How to get session value using javascript
I have a class to deal with session variables. 我有一个类来处理会话变量。 Here is a sample attached:
这是附件样本:
namespace General
{
public class Session
{
public Session()
{
}
public static string UserID
{
get { return HttpContext.Current.Session["UserID"] as string ?? String.Empty; }
set { HttpContext.Current.Session["UserID"] = value; }
}
public static string departFlightID
{
get { return HttpContext.Current.Session["departFlightID"] as string ?? String.Empty; }
set { HttpContext.Current.Session["departFlightID"] = value; }
}
public static string returnFlightID
{
get { return HttpContext.Current.Session["returnFlightID"] as string ?? String.Empty; }
set { HttpContext.Current.Session["returnFlightID"] = value; }
}
}
}
Now at some point I store the flightID
in: 现在在某些时候我将
flightID
存储在:
General.Session.departFlightID = flightID;
And at another point I want to retrieve this value using Javascript. 另一方面,我想使用Javascript检索此值。 I have seen examples here but they won't work (there's no error but they will return
EMPTY
). 我在这里看过这些例子,但它们不起作用(没有错误,但它们将返回
EMPTY
)。
Most recent tries: 最近的尝试:
var session = '<%= General.Session.departFlightID %>';
var session = '<%= HttpContext.Current.Session["departFlightID"] %>';
It does work if I set value on page load, but I am running a webmethod where I create a html and then send that html back to be populated inside a div to make a ticket there. 如果我在页面加载上设置值,它确实有效,但是我正在运行一个web方法,我在其中创建一个html,然后将该html发送回到div中,以便在那里制作一张票。 I need to get the session value but it does not update.
我需要获取会话值但它不会更新。
To make it more clear here is the Javascript code: 为了使其更清楚,这里是Javascript代码:
<script type="text/javascript">
function addTicket(flightID, flightType) {
PageMethods.addTicket(flightID, flightType, OnGetMessageSuccess);
}
function OnGetMessageSuccess(result, userContext, methodName) {
var pageUrl = document.URL;
if (pageUrl.indexOf("&ret_date=&") !== -1) {
var session = '<%= HttpContext.Current.Session["departFlightID"] %>';
alert(session);
result = result + "<a onclick=\"searchflight.abortAllAjax()\" data-index=\"6\" class=\"bookNow\" title=\"Book Now!\" href=\"#\">Book Now!</a>";
result = result + "</div> </div> </div>";
document.getElementById("detail").innerHTML = result;
}
}
And here is the webmethod: 这是网络方法:
[System.Web.Services.WebMethod]
public static string addTicket(string flightID, string flightType)
{
//GET FLIGHT DETAILS FROM DATATABLE OR SESSION DATA TABLE
string tmpHtml = string.Empty;
tmphtml="sample";
string flightID="123123213";
General.Session.departFlightID=flightID;
return tmphtml;
}
This is the same Question as here . 这是与此处相同的问题。
we usually have sensitive data in Session Variables, so they are always server side. 我们通常在会话变量中有敏感数据,所以它们总是服务器端。 Still if you want you can try adding a HiddenField in your .aspx markup and in code behind you can set it .
如果你想要,你仍然可以尝试在.aspx标记中添加一个HiddenField,在后面的代码中你可以设置它。 once the value is set, you can very easily access the value using $("#id") way or Document.GetElementById() method.
设置值后,您可以使用$(“#id”)方式或Document.GetElementById()方法轻松访问该值。
如果您使用的是mvc,那么:
var ttype = ' @HttpContext.Current.Session["TestType"] ';
WebMethods do not support Session variables by default. WebMethods默认不支持Session变量。 Your WebMethod declaration should look like this:
您的WebMethod声明应如下所示:
[WebMethod(EnableSession = true)]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.