信息内容
您现在的位置:首页-信息内容
Asp.net实现无刷新调用后台实体类数据并以Json格式返回 发布日期:2018-5-11 9:23:01 来源:csdn 【关闭】

新建一般处理程序

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
publicclassTemp
{
  publicintIndex {get;set; }
  publicstringDescription {get;set; }
  publicstringImagePath {get;set; }
  publicDateTime MyDate {get;set; }
}
 
//数据源
 List<Temp> listTemp =newList<Temp>()
 {
   newTemp(){ Index=1,ImagePath="Desert.jpg", Description="图片1",MyDate=DateTime.Now},
   newTemp(){Index=2,ImagePath="Chrysanthemum.jpg", Description="图片2",MyDate=DateTime.Now},
   newTemp(){Index=3,ImagePath="Penguins.jpg", Description="图片3",MyDate=DateTime.Now},
   newTemp(){Index=4,ImagePath="Jellyfish.jpg", Description="图片4",MyDate=DateTime.Now},
   newTemp(){Index=5,ImagePath="Tulips.jpg", Description="图片5",MyDate=DateTime.Now}
 };
  
 publicvoidProcessRequest(HttpContext context)
 {
   stringindex = context.Request["Index"];
   stringjsonStr =string.Empty;
   foreach(var iteminlistTemp)
   {
     if(item.Index.ToString() == index)
     {
       JavaScriptSerializer serializer =newJavaScriptSerializer();
       jsonStr = serializer.Serialize(item);//序列化为json格式
       break;
     }
   }
  
   context.Response.Write(jsonStr);
 }

前台JS代码

?
1
2
3
4
5
6
7
$.getJSON("imageChange.ashx", { Index: i - 1 },function(result) {
  $("#<%=lblDescription.ClientID %>").text(result.Description);
$("#<%=Image1.ClientID %>").attr("src", path + result.ImagePath.substr(result.ImagePath.lastIndexOf('/') + 1));
  
  vard = eval("new "+ result.MyDate.replace(/\//g, ""));
          $("#<%=lblDate.ClientID %>").text(Todate(d.ToLocalTime().toString()));
});

前台JS代码
//ToLocalTime()将UTC格式数据转换成标准日期格式
//注意JavaScriptSerializer会将日期序列号为自1970年1月1号的刻度值,所以js获取的时间值需做一些处理转换成标准日期格式

//详见http://msdn.microsoft.com/zh-cn/library/system.web.script.serialization.javascriptserializer.aspx



成都世帆软件    交通运输综合执法系统软件、公路路政执法系统软件、道路运政执法系统软件、海事行政、水路运政、航道行政、港口行政执法 管理系统软件定制开发

交通综合执法系统、公路路政执法管理系统、超限运输管理系统、道路运政执法管理系统、运管执法管理系统、公路养护管理系统、公路运行监测系统
市场监管执法软件、工商行政执法软件、环境监察执法软件、城市管理执法软件、食品卫生执法软件

备案号:蜀ICP备12010221号-20    电-话:028-8768 9944