weixin_33714884 2018-10-04 09:03 采纳率: 0%
浏览 45

select2多个ajax调用

I'm trying to call 2 REST APIs and then populate the select2 dropdown. My select2 code works fine for 1 API call but doesn't if I try to call the 2 API in AJAX results.

The way I'm trying to call the second API looks a but dicey to me but I'm not too sure how do I achieve this. I can nest AJAX calls if not using them in select2 but this is a bit peculiar case.

Any idea on how to achieve that/ or what am I doing wrong here

below is the code for that:

Select2({
       disabled : true,
       minimumInputLength : 1,
       multiple : true,
       ajax : {
           url : "/rest/api/2/user/picker" ,
           type : "GET",
            dataType : 'json',
            cache : true,
            // query parameters for the remote ajax call
            data : function data(term) {
                       return {
                          query : term,
                          maxResults : 5,
                          showAvatar : true
                       };
           },
           // parse data from the server into form select2 expects
           results : function results(data) {
           var i, dataLength, dataUsers;
           data = JSON.parse(data);
           var users = [];                      
           dataLength = data.users.length;
           dataUsers = data.users;
           for (var i = 0; i < dataLength; i++) 
            {
              if (dataUsers[i].key != $scope.userInputs.owner.key) {
                  dataUsers[i].context = CommonConstants.RECIPIENT_AUTH_CONTEXT.USER;
                users.push(dataUsers[i]);
              }
            }
            if($scope.someBoolean){
                 ajax : {
                      url : "/rest/api/2/groups/picker",
                      type : "GET",
                      dataType : 'json',
                      cache : true,
                      data : function data(term) {
                               return {
                                  query : term,
                                  maxResults : 5
                               };
                      },
                      success: function(responseForGroups){
                         var groupData = JSON.parse(responseForGroups);
                         var dataGroups, groupsLength;
                         dataGroups = groupData.groups;
                         groupsLength = groupData.total;

                         for (var i = 0; i < groupLength; i++) 
                         {
                            dataGroups[i].context = CommonConstants.RECIPIENT_AUTH_CONTEXT.GROUP;
                            dataGroups[i].key = dataGroups[i].name;
                            dataGroups[i].displayName = dataGroups[i].name;
                            users.push(dataGroups[i]);
                         }    
            }
       };
 }
 return {
     results : users
 };
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
    • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
    • ¥15 手机接入宽带网线,如何释放宽带全部速度
    • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
    • ¥15 ETLCloud 处理json多层级问题
    • ¥15 matlab中使用gurobi时报错
    • ¥15 这个主板怎么能扩出一两个sata口
    • ¥15 不是,这到底错哪儿了😭
    • ¥15 2020长安杯与连接网探
    • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么