Get the session in html file using js

Source: Internet
Author: User

It was actually simple. But I still encountered a problem that was not taken into account before I did it: the ajax cache problem. Therefore, the seemingly easy problem should be taken strictly. This is the quality that programmers should have.

Let's not talk much about it. The following is what happens during runtime.


H2. file description

Default. aspx, main demo page

GetSessionData. aspx: ajax call page

I am familiar with jquery. js and js frameworks.


H2. code analysis

The code is very simple. It mainly focuses on the cache of ajax, that is, you have set a new session value, but the old value is obtained by using ajax. In depth, this is not just an ajax cache problem, it should be related to the http protocol itself

There are many ways to invalidate the ajax cache. Here we use the server side to set GetSessionData. aspx. cs
Using System;
Using System. Data;
Using System. Configuration;
Using System. Collections;
Using System. Web;
Using System. Web. Security;
Using System. Web. UI;
Using System. Web. UI. WebControls;
Using System. Web. UI. WebControls. WebParts;
Using System. Web. UI. HtmlControls;
Public partial class GetSessionData: System. Web. UI. Page
{
Protected void Page_Load (object sender, EventArgs e)
{
Response. Cache. SetCacheability (HttpCacheability. NoCache );
Response. Write (Session ["current-date"]);
}
}


Note Line 16: Response. Cache. SetCacheability (HttpCacheability. NoCache );

This line of code allows the client to re-read from the server without using the cache.

I hope this article will help you.

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.