Problem:
In asp.net, how does one clear Session objects using JS?
My demand is now like this. I need to clear the Session object using JS scripts. I have found a lot on the internet, saying that the client cannot operate the Server Object. Please kindly advise me. Thank you.
Answer:
You can use JS to call the server to process files to clear Session objects.
HTML:
<Head>
<Meta http-equiv = "Content-Type" content = "text/html; charset = gb2312"/>
<Title> untitled document </title>
<Script type = "text/javascript">
Var xmlhttp;
Function createXmlhttp (){
If (window. XMLHttpRequest ){
Xmlhttp = new XMLHttpRequest ();
}
Else if (window. ActiveXObject ){
Try {
Xmlhttp = new ActiveXObject ("Msxml2.XMLHTTP ");
} Catch (e ){
Xmlhttp = new ActiveXObject ("Microsoft. XMLHTTP ");
}
}
Return xmlhttp;
}
Function ClearSession ()
{
CreateXmlhttp ();
Var url = "Service. asmx/ClearSession ";
Xmlhttp. open ("POST", url, true );
Xmlhttp. onreadystatechange = handleStateChange;
Xmlhttp. setRequestHeader ("Content-Type", "application/x-www-form-urlencoded ;");
Xmlhttp. send (queryString );
}
Function handleStateChange ()
{
If (xmlhttp. readyState = 4)
{
If (xmlhttp. status = 200)
{
// Cleared successfully
}
}
}
</Script>
</Head>
<Body>
<Input name = "Submit" type = "button" onclick = "ClearSession ();" value = "Clear Session"/>
</Body>
WEB Services:
<% @ WebService Language = "C #" class = "Service" %>
Using System;
Using System. Web;
Using System. Web. Services;
Using System. Web. Services. Protocols;
Using System. Web. SessionState; // The Session cannot be operated unless introduced.
[WebService (Namespace = "http://tsingjun.cn/")]
[WebServiceBinding (ConformsTo = WsiProfiles. BasicProfile1_1)]
Public class Service: System. Web. Services. WebService
{
[WebMethod (true)]
Public int ClearSession ()
{
// Clear the Session code
Return 0;
}
}