【问题标题】:Merging the results of two GET Rest APIs into a single JSON list将两个 GET Rest API 的结果合并到一个 JSON 列表中
【发布时间】:2018-02-01 13:46:15
【问题描述】:

我目前正试图合并来自两个 GET REST API 调用的两个 JSON 列表。

第一个 API 调用获取尝试访问该网页的用户的用户 ID。

后两个 url 每个都返回一个特定安全组中的用户 ID 列表。 我需要一个将两个 JSON 列表的值合并到一个列表中的列表。

如果访问网页的用户 ID 不在 either 安全组的用户 ID 列表中,则页面内容对他们隐藏。用户只需在两个安全组之一中即可访问网页。

到目前为止,我所做的尝试使用户需要在两个安全组中才能访问,而实际上他们只需要在一个中.我可能会以错误的方式解决这个问题。这可以变成 if elseif 语句吗?有任何想法吗?

var submitterId = null;
    var userIdUrl = '/api/HttpCurrentUser';
    var Url1 = '/api/ActiveDirectoryMembers/example1';
    var Url2 = '/api/ActiveDirectoryMembers/example2';
    $("#masterContainer").hide();
    $.get(userIdUrl).done(function (result) {
        submitterId = result;
        isInGroup = false;
        $.get(Url1).done(function (result) {
            result.forEach(function (item) {
                if (item == submitterId) {
                    $("#masterContainer").show();
                    isInGroup = true;
                }
            });
            if (!isInGroup) {
                $("#masterContainer").replaceWith("Access to this site is restricted to the example1 and example2 groups);
            }
        });
        $.get(Url2).done(function (result) {
            result.forEach(function (item) {
                if (item == submitterId) {
                    $("#masterContainer").show();
                    isInGroup = true;
                }
            });
            if (!isInGroup) {
                $("#masterContainer").replaceWith("Access to this site is restricted to example 1 and example 2 groups");
            }
        });

        });

【问题讨论】:

    标签: jquery json ajax rest


    【解决方案1】:

    修改了您的代码,并在编辑处添加了 cmets:

    var submitterId = null;
        var userIdUrl = '/api/HttpCurrentUser';
        var Url1 = '/api/ActiveDirectoryMembers/example1';
        var Url2 = '/api/ActiveDirectoryMembers/example2';
        var mergedResult; // a variable to hold both results
        $("#masterContainer").hide();
        $.get(userIdUrl).done(function (result) {
          submitterId = result;
          isInGroup = false;
          $.get(Url1).done(function (result) {
            mergedResult = result;
          });
          $.get(Url2).done(function (result) {
            $.extend(mergedResult, result); // will merge the result, into mergedResult (the first target)
          });
    
          mergedResult.forEach(function (item) {
            if (item == submitterId) {
              $("#masterContainer").show();
              isInGroup = true;
            }
          });
    
          if (!isInGroup) {
            $("#masterContainer").replaceWith("Access to this site is restricted to example 1 and example 2 groups");
          }
    
        });
    

    基本上,使用extend 来合并结果。

    【讨论】:

    • 我在 mergeResult.forEach(function (item){} 处收到“未捕获的类型错误:无法读取未定义的属性 'forEach'”
    • @AngelaDylan console.log(mergedResult) 得到的结果是什么?传递来自 Url1 和 Url2 的结果,例如:result.json()。
    • 我得到一个包含 218 个 id 的数组返回
    • @AngelaDylan 很奇怪,你应该可以使用forEach 循环数组。是否有可能发布 Url1 返回的响应或 json 的结构?
    • @AngelaDylan 如果响应是数组类型,请使用$.merge 而不是$.extendmerge
    猜你喜欢
    • 1970-01-01
    • 2022-09-28
    • 1970-01-01
    • 1970-01-01
    • 2017-08-15
    • 2019-07-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多