做了MVC模式一年,可不要把传统模式忘记呀!ashx配合aspx实现AJAX加载JSON数据

ASHX内容:
 #region IHttpHandler Members
 
 
        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "application/x-javascript";
 
            List<User> list = new List<User>();
            for (int i = 0; i < 10; i++)
            {
                list.Add(new User { UserName = "zzl" + i, Tel = "13521972991" });
            }
 
            string str = "[";
            list.ForEach(i =>
            {
                str += "{" + string.Format("\"UserName\":\"{0}\", \"Telephone\":\"{1}\"", i.UserName, i.Tel) + "},";
 
            });
            str = str.TrimEnd(',') + "]";
            context.Response.Write(str);
        }
 
#endregion
ASPX内容:
 function bindDatajQueryAjaxJson() {
            $.ajax({
                url: "Handler2.ashx",
                dataType: "json",
                cache: false,
                data: null,
                type: "GET",
                success: function (data) {
                    var msg = "";
                    for (var i = 0, length = data.length; i < length; i++) {
                        msg += "<Div style='color:red'>UserName:" + data[i].UserName + ",Telephone:" + data[i].Telephone + "</div>";
                    }
                    $("#msg2").html(msg);
                }
            });
        }
       bindDatajQueryAjaxJson();
       

你可能感兴趣的:(json)