惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

量子位
GbyAI
GbyAI
V
Vulnerabilities – Threatpost
阮一峰的网络日志
阮一峰的网络日志
罗磊的独立博客
Recorded Future
Recorded Future
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
博客园 - 司徒正美
Y
Y Combinator Blog
Microsoft Security Blog
Microsoft Security Blog
美团技术团队
博客园 - Franky
Blog — PlanetScale
Blog — PlanetScale
B
Blog RSS Feed
V
Visual Studio Blog
Martin Fowler
Martin Fowler
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
博客园_首页
C
Cybersecurity and Infrastructure Security Agency CISA
博客园 - 叶小钗
AWS News Blog
AWS News Blog
Project Zero
Project Zero
T
Threat Research - Cisco Blogs
V
V2EX
F
Fortinet All Blogs
The GitHub Blog
The GitHub Blog
Latest news
Latest news
N
News and Events Feed by Topic
The Last Watchdog
The Last Watchdog
T
Threatpost
L
Lohrmann on Cybersecurity
小众软件
小众软件
IT之家
IT之家
MongoDB | Blog
MongoDB | Blog
博客园 - 聂微东
Engineering at Meta
Engineering at Meta
爱范儿
爱范儿
Google Online Security Blog
Google Online Security Blog
Forbes - Security
Forbes - Security
Attack and Defense Labs
Attack and Defense Labs
The Register - Security
The Register - Security
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
H
Help Net Security
Security Latest
Security Latest
Recent Announcements
Recent Announcements
C
Check Point Blog
B
Blog
Google DeepMind News
Google DeepMind News
K
Kaspersky official blog
I
InfoQ

博客园 - andy.wu

聊聊除了生产率之外的东西,比如赚钱效率 一次思维锻炼,使用拼音模糊匹配中文 - andy.wu - 博客园 一些需要解决的问题(Win32) svn howto: svn合并时报错:retrieval of mergeinfo unsupported by 。 Windows Mobile应用开发(1):使用Win32 SDK 开发屏幕手电程序 vs(2005 and 2008)中使用vc++创建智能设备项目失败的正确解决方案 思考:google的opensocial的实现原理 思考:日期类型的数据应该用什么样的具体形式存储到数据库? google got crazy!!! EnableViewState详细分析 - andy.wu - 博客园 NHibernate Tips: 要注意模型与数据库在Null方面的匹配 AjaxPro基础知识 and FAQ 在iis 6中使用共享目录作为虚拟目录 初学wpf感想 Sql Server 2005全文检索中碰到的问题和分析 给博客园首页管理的建议 china-pub,当当,卓越购书经验谈 使用Emacs代替Windows下的Command shell 经验:使用.net 2.0中的TransactionScope碰到的问题
asp.net faq: 在html文件中,用js获取session?
andy.wu · 2008-07-08 · via 博客园 - andy.wu

好久没去csdn了,逛过去想作些贡献,速度实在是不能忍受,还是逃回来了,呵呵。对比一下淘宝,虽然不太清楚两者的负载是否是一个档次,但作为一个技术网站,这种表现实在是太差了!

不过csdn的发帖量果然巨大,我只看了一下论坛的asp.net板块,每秒会有3-5个回复,真是热闹非凡啊。

在逃回来之前,在新手区看到了一个提问,原标题为“ 在html文件中,用js获取session?”,回答的人十分踊跃,不过风格十分csdn:正确但无用,真是言简意赅。我也是从菜鸟过来的,深知大师的指点固然重要,但要是有一个实际的例子,那才是最实用的。于是决定做一例子,顺便锻炼一下自己的语文,:)

原以为很简单,事实上的确简单。但还是碰到了一个在做之前没有考虑到的问题:ajax的缓存问题。所以看似容易的问题还是要严谨对待,这就是程序员该有的品质吧。

话不多说,言规正转,下面是运行时的截图
080708-1.JPG

点这里下载示例代码

h2. 文件说明

  • default.aspx, 主演示页面
  • GetSessionData.aspx, ajax调用的页面
  • jquery.js, js框架,我对这个熟悉一些

h2. 代码分析

  • 代码很简单,主要是注意ajax的缓存问题,即你明明设置了新的session值,但使用ajax得到的却还是老的值。深入的说,这并非只是ajax的缓存问题,应该是和http协议本身相关的
  • 让ajax缓存失效有很多办法,我这里采用的是服务器端设置

    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"]);
        }
    }
    
    

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

    这一行的代码可以让客户端不使用缓存,而从服务器重新读取

    希望本文对有需要的朋友有所帮助。