如何从POST ajax响应中捕获特定值?

时间:2013-08-20 16:43:05

标签: c# javascript jquery ajax asp.net-mvc

请参阅ajax成功函数中的注释:

function CreateProjectTree(sc)
{
    $.ajax({
        type: "POST",
        url: "../api/projects/SearchProjects",
        data: sc,
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function(data)
        {
            if ($(data).length === 1)
            {
                window.location.replace("../ViewProjectDetails.aspx?ProjectId=" + //here's where I need to get the id;
            }
            else
            {
                buildTree(data);
            }
        },
    });
}

这就是控制器对帖子的看法:

public class ProjectsController : ApiController
{
    public List<Item> SearchProjects(GBLProjectSearchCriteria searchCriteria)
    {
        var ProjectSearchResult = new ProjectSearchResultController();
        searchCriteria.SearchType = "P";
        searchCriteria.QueryString = "?ProjectId=";
        var GBLProjectSearchResultListData = ProjectSearchResult.GetProjectSearchResultList(searchCriteria);
        var projectList = (from GBLProjectSearchResult item in GBLProjectSearchResultListData
                           select new Item
                           {
                               Id = item.Id,
                               Title = item.Title,
                               Url = item.NavigateUrl + item.QueryString
                           }).ToList();

        foreach (var project in projectList)
        {
            //seasons
            project.Items = new List<Item>();
            var SeasonSearchResult = new ProjectSearchResultController();
            searchCriteria.Id = project.Id;
            searchCriteria.SearchType = "S";
            searchCriteria.QueryString = "?ProjectId=" + project.Id + "&SeasonId=";
            var GBLSeasonSearchResultListData = SeasonSearchResult.GetProjectSearchResultList(searchCriteria);
            foreach (var season in from GBLProjectSearchResult item in GBLSeasonSearchResultListData
                                   select new Item
                                   {
                                       Id = item.Id,
                                       Title = item.Title,
                                       Url = item.NavigateUrl + item.QueryString
                                   })
            {
                project.Items.Add(season);
                project.HasChildren = (project.Items.Count > 0);
            }
            foreach (var season in project.Items)
            {
                //episodes
                season.Items = new List<Item>();
                var episodeSearchResult = new ProjectSearchResultController();
                searchCriteria.Id = season.Id;
                searchCriteria.SearchType = "E";
                searchCriteria.QueryString = "?ProjectId=" + project.Id + "&SeasonId=" + season.Id + "&EpisodeId=";
                var GBLEpisodeSearchResultListData = episodeSearchResult.GetProjectSearchResultList(searchCriteria);
                foreach (GBLProjectSearchResult item in GBLEpisodeSearchResultListData)
                {
                    var episode = new Item
                    {
                        Id = item.Id,
                        Title = item.Title,
                        Url = item.NavigateUrl + item.QueryString
                    };
                    season.Items.Add(episode);
                    season.HasChildren = (season.Items.Count > 0);
                }
            }
        }
        return projectList;
    }

    public class Item
    {
        readonly string take2Root = ConfigurationManager.AppSettings["Take2Root"];
        private string url;

        public int Id
        {
            get;
            set;
        }

        public string Title
        {
            get;
            set;
        }

        public bool HasChildren
        {
            get;
            set;
        }

        public List<Item> Items
        {
            get;
            set;
        }

        public string Url
        {
            get
            {
                return url;
            }
            set
            {
                url = take2Root + value.Replace("..", "");
            }
        }
    }
}

我需要获取此控制器返回的父节点的ID。

我知道如何使用GET请求,但是,我必须在这里进行POST,我不确定如何在ajax成功函数中提取ID。

有人可以帮我一把吗?

谢谢!

1 个答案:

答案 0 :(得分:0)

if ($(data).length === 1)语句中,您可以添加debugger;(或设置断点),然后使用chrome的开发人员工具检查数据对象。您所追踪的ID将类似于data.objects[0].id