标签:sse 信息 on() context set eric system html charset
今天写程序得时候遇到了一个问题:ajax在对ashx进行请求时如果按照 context.Request方式直接来获取值得话获取到得是空值,因此去网上搜了一下问题。现记录如下:
ashx获取session值:
1.首先添加引用:using System.Web.SessionState;
2.我们得一般处理程序类要继承IRequiresSessionState接口
3.对session值判断是否为null
4.使用context.session["***"] 得到对应得session值
下面写一个例子测试一下:
html代码:
1 <!DOCTYPE html> 2 <html xmlns="http://www.w3.org/1999/xhtml"> 3 <head> 4 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 5 <title>测试界面</title> 6 <script src="javascript/jquery-1.11.1.min.js"></script> 7 <style> 8 </style> 9 <script> 10 //测试ajax向后台获取信息 11 function getinfo() { 12 $.ajax({ 13 url: "ashx/Handler1.ashx?action=get", 14 dataType: "text", 15 success: function (data) { 16 alert(data); 17 }, 18 }) 19 } 20 function setsession() { 21 $.ajax({ 22 url: "ashx/Handler1.ashx?action=set", 23 }) 24 } 25 window.onload = function () { 26 setsession(); 27 getinfo(); 28 } 29 </script> 30 </head> 31 <body> 32 <div id="test"> 33 </div> 34 </body> 35 </html>
ashx代码:
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Web; 5 using System.Web.SessionState; 6 namespace Biobase_BigData.ashx 7 { 8 /// <summary> 9 /// Handler1 的摘要说明 10 /// </summary> 11 public class Handler1 : IHttpHandler, IRequiresSessionState 12 { 13 14 public void ProcessRequest(HttpContext context) 15 { 16 context.Response.ContentType = "text/plain"; 17 string action = ""; 18 if (context.Request.QueryString["action"] != null) 19 { 20 action = context.Request.QueryString["action"].ToString(); 21 } 22 switch (action) 23 { 24 case "get": 25 string session = ""; 26 if(context.Session["name"]!=null){ 27 session = context.Session["name"].ToString(); 28 } 29 context.Response.Write(session); 30 break; 31 case "set": 32 context.Session["name"] = "ceshi"; 33 break; 34 } 35 } 36 37 public bool IsReusable 38 { 39 get 40 { 41 return false; 42 } 43 } 44 } 45 }
运行结果显示:
标签:sse 信息 on() context set eric system html charset
原文地址:http://www.cnblogs.com/lsbk/p/7783137.html