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

时间:2022-02-08 03:40:49

后台传JSON

 public class CourseType : IHttpHandler
{
Epoint.PeiXun.Bizlogic.BLL.CourseLibrary.PX_CourseType.B_PX_CourseType b_coursetype = new Epoint.PeiXun.Bizlogic.BLL.CourseLibrary.PX_CourseType.B_PX_CourseType(); List<CourseTypeData> list_CourseType = new List<CourseTypeData>();
CourseTypeChartData CourseTypecd = new CourseTypeChartData();
public void ProcessRequest(HttpContext context)
{
string rjson = string.Empty; try
{
int Count1;
string where = "where 1=1";
DataView dv = Epoint.MisBizLogic2.DB.GetData_Page_ByConnName(
"*",
,
,
" PX_CourseType ",//Frame_AttachInfo和NTI_CourseChapter
"Row_ID",
where,
"ParentRowID asc,OrderNum desc",
out Count1,
"DJG_PeiXun_ConnectionString"
).DefaultView;
CourseTypecd.PicNum = Convert.ToString(dv.Count);
for (int i = ; i < dv.Count; i++)
{
list_CourseType.Add(new CourseTypeData(Convert.ToString(dv[i]["TypeName"].ToString()), Convert.ToString(dv[i]["parentRowID"].ToString()), Convert.ToString(dv[i]["Row_ID"].ToString())));
}
CourseTypecd.CourseData = list_CourseType;
rjson = new Epoint.KSPXBase.Bizlogic.DB_Common().Obj2Json(CourseTypecd); }
catch
{
rjson = "";
} context.Response.ContentType = "text/plain";
context.Response.Write(rjson);//返回调用处
} public bool IsReusable
{
get
{
return false;
}
} internal class CourseTypeData
{
public CourseTypeData(string typeName, string parentRowID,string row_id)
{
TypeName = typeName;//类别名称
ParentRowID = parentRowID;
Row_ID = row_id;
}
public string TypeName;
public string ParentRowID;
public string Row_ID;
} internal class CourseTypeChartData
{
public string PicNum;//数据个数
public List<CourseTypeData> CourseData;
}
}

前台解析,并拼接显示

 $(function () {//加载课程推荐
LoadCourseTypeName($("#CourseType"));//加载课程
}); function LoadCourseTypeName(obj) {
$.ajax({
type: "POST",
contentType: "application/json;charset=utf-8",
url: "<%=Request.ApplicationPath%>/Ashx/CourseType.ashx?",
dataType: 'text',
complete: function () { },
beforeSend: function () {
obj.html("<div class='divLoadTips'>课程内容加载中...</div>");
},
success: function (result) {
var res = JSON.parse(result);//得到json值
var rtn = parseInt(res.PicNum);
obj.html("")
var span, a;
for (var i = ; i < rtn; i++) {
var parentRowID = res.CourseData[i].ParentRowID;
if (parentRowID == "") {
var row_id = res.CourseData[i].Row_ID;
var li = $("<li class='drop-menu-item' id='" + row_id + "'> </li>");
span = $("<span class='drop-menu-cate' >" + res.CourseData[i].TypeName + "</span>")
li.append(span);
}
else {
$("#menucourse ul li").each(
function (index) { if ($(this).attr("id") == parentRowID) {
a = $("<a href='#'> " + res.CourseData[i].TypeName + "</a>");
$(this).append(a);
}
}
); } obj.append(li);
}
},
error: function (result, status) {
alert(result.responseJSON.Message);
}
})
}