一般处理程序返回json

一般处理程序:
 
 public void ProcessRequest(HttpContext context)
        {
            string action = context.Request.Params["action"];
            switch (action)
            {
                case "TestAction":
                    GetJsonString();
                    break;
            }
            context.Response.ContentType = "text/plain";
        }
        public void GetJsonString()
        {
            Product product = new Product();
            product.Name = "Apple";
            product.Expiry = new DateTime(2008, 12, 28);
            product.Sizes = new string[] { "Small" };
            List<Product> list = new List<Product>();
            for (int i = 0; i < 12; i++)
            {
                list.Add(product);
            }
            HttpContext.Current.Response.Write(JsonConvert.SerializeObject(new { Result = 1, Msg = "获取数据成功", Data = list }));
     }

 
第一次加载js:
$(function () {
        $.ajax({
            type: "get",
            url: "/action/Handler.ashx",
            data: { action: "TestAction" },
            dataType: "json",
            success: function (data) {
                alert(data.Result);
                alert($.isArray( data.Data));
            },
            error: function (XMLHttpRequest, textStatus, errorThrown) {
                alert(XMLHttpRequest.status);
                alert(XMLHttpRequest.readyState);
                alert(textStatus);
            }
        });
    })

  

posted @ 2019-10-17 15:34  大da脸  阅读(590)  评论(0编辑  收藏  举报