How to clear Session objects using JS in asp.net

Source: Internet
Author: User

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;
}
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.