后台给前台传JSON字符串,前台解析并拼接显示

后台传JSON

 1 public class CourseType : IHttpHandler
 2     {
 3         Epoint.PeiXun.Bizlogic.BLL.CourseLibrary.PX_CourseType.B_PX_CourseType b_coursetype = new Epoint.PeiXun.Bizlogic.BLL.CourseLibrary.PX_CourseType.B_PX_CourseType();
 4 
 5         List<CourseTypeData> list_CourseType = new List<CourseTypeData>();
 6         CourseTypeChartData CourseTypecd = new CourseTypeChartData();
 7         public void ProcessRequest(HttpContext context)
 8         {
 9             string rjson = string.Empty;
10 
11             try
12             {
13                 int Count1;
14                 string where = "where 1=1";
15                 DataView dv = Epoint.MisBizLogic2.DB.GetData_Page_ByConnName(
16                    "*",
17                    100,
18                    1,
19                    " PX_CourseType ",//Frame_AttachInfo和NTI_CourseChapter
20                    "Row_ID",
21                    where,
22                    "ParentRowID asc,OrderNum desc",
23                    out Count1,
24                    "DJG_PeiXun_ConnectionString"
25                    ).DefaultView;
26                 CourseTypecd.PicNum = Convert.ToString(dv.Count);
27                 for (int i = 0; i < dv.Count; i++)
28                 {
29                     list_CourseType.Add(new CourseTypeData(Convert.ToString(dv[i]["TypeName"].ToString()), Convert.ToString(dv[i]["parentRowID"].ToString()), Convert.ToString(dv[i]["Row_ID"].ToString())));
30                 }
31                 CourseTypecd.CourseData = list_CourseType;
32                 rjson = new Epoint.KSPXBase.Bizlogic.DB_Common().Obj2Json(CourseTypecd);
33 
34             }
35             catch
36             {
37                 rjson = "0";
38             }
39 
40             context.Response.ContentType = "text/plain";
41             context.Response.Write(rjson);//返回调用处
42         }
43 
44         public bool IsReusable
45         {
46             get
47             {
48                 return false;
49             }
50         }
51 
52         internal class CourseTypeData
53         {
54             public CourseTypeData(string typeName, string parentRowID,string row_id)
55             {
56                 TypeName = typeName;//类别名称
57                 ParentRowID = parentRowID;
58                 Row_ID = row_id;
59             }
60             public string TypeName;
61             public string ParentRowID;
62             public string Row_ID;
63         }
64 
65 
66         internal class CourseTypeChartData
67         {
68             public string PicNum;//数据个数
69             public List<CourseTypeData> CourseData;
70         }
71     }
View Code

前台解析,并拼接显示

 1 $(function () {//加载课程推荐
 2         LoadCourseTypeName($("#CourseType"));//加载课程
 3     });
 4 
 5     function LoadCourseTypeName(obj) {
 6         $.ajax({
 7             type: "POST",
 8             contentType: "application/json;charset=utf-8",
 9             url: "<%=Request.ApplicationPath%>/Ashx/CourseType.ashx?",
10             dataType: 'text',
11             complete: function () { },
12             beforeSend: function () {
13                 obj.html("<div class='divLoadTips'>课程内容加载中...</div>");
14             },
15             success: function (result) {
16                 var res = JSON.parse(result);//得到json值
17                 var rtn = parseInt(res.PicNum);
18                 obj.html("")
19                 var span, a;
20                 for (var i = 0; i < rtn; i++) {
21                     var parentRowID = res.CourseData[i].ParentRowID;
22                     if (parentRowID == "0") {
23                         var row_id = res.CourseData[i].Row_ID;
24                         var li = $("<li class='drop-menu-item' id='" + row_id + "'> </li>");
25                         span = $("<span class='drop-menu-cate' >" + res.CourseData[i].TypeName + "</span>")
26                         li.append(span);
27                     }
28                     else {
29                         $("#menucourse ul li").each(
30                             function (index) {
31 
32                                 if ($(this).attr("id") == parentRowID) {
33                                     a = $("<a href='#'> " + res.CourseData[i].TypeName + "</a>");
34                                     $(this).append(a);
35                                 }
36                             }
37                             );
38  
39                     }
40 
41                     obj.append(li);
42                 }
43             },
44             error: function (result, status) {
45                 alert(result.responseJSON.Message);
46             }
47         })
48     }
View Code
原文地址:https://www.cnblogs.com/lyhsblog/p/5966174.html