Jquery读取Json并解析

客户端访问服务器后ACTION中的代码:
[Description("根据父亲ID获取子菜单列表")]
        public ActionResult GetSubMenuItem(int ParentID)
        {
            WebCache cache = new WebCache();
            var u = (CurrentUser)cache.GetSessionCache("currentuser");
            foreach (var subMenu in u.MenuPermissions.Where(menu => menu.ParentId == ParentID))
            {
                String menuName = subMenu.Name;
                String menuUrl = subMenu.Url;
            }
            var json = from subMenu in u.MenuPermissions
                       where subMenu.ParentId == ParentID
                       select new
                       {
                           name = subMenu.Name,
                           url = subMenu.Url
                       };
            return Json(json);
        }
       
我们在这里组装成的JSON的格式为:
[{"name":"菜单管理","url":"/SysDictionary/Index"}]

客户端以AJAX方式提交请求,并处理JSON格式的方法:
 function selectSubMenu(ParentID) {
        var menuItem = "";
        var oJson = {
            ParentID: escape(ParentID)
        };
        var url = "/Home/GetSubMenuItem";
        $.ajax({
            url: url,
            data: oJson,
            dataType: "json",
            cache: false,
            type: "POST",
            success: function (data) {
                $.each(data, function (i, item) {
                    alert(item.name+item.url);
                });
            },
            error: function () {
                ShowMessage("error", data.message);

            }
        });
    }

你可能感兴趣的:(jquery,Ajax,json,cache)