繁体   English   中英

我想将数据存储在Web API的数组中

[英]I want to store the data in an array from the web api

我想使用来自Web api的值,一切运行良好,但是当我从单个变量中的url读取值时,它作为大数据出现:

 var orguni = "http://hospdev.hispindia.org/haryana_220/api/organisationUnits?fields=name,id,code"; $.ajax({ type: "GET", url: orguni, dataType: "xml", crossDomain: true, headers: { }, success: function (xml) { $(xml).find('organisationUnit ').each(function () { var ou = $(this).attr('id'); console.log("UID: "+ ou); }); } }); 

现在,当我显示ou它返回一个不同ID的列表,并且我想将所有ID用作单独的值,有人可以告诉我该怎么做。

Currently it returns:

index.html:26 UID:  g8cMOWx5ydN
index.html:26 UID:  Q2FEgPgHvMr
index.html:26 UID:  XpIf2v7cJRX
index.html:26 UID:  uoPA7guOuLa
index.html:26 UID:  BLpdsMuZcqD

当我使用console.log(ou [0]);时 它从每个类似的对象返回1个字符:

g
Q
x
...
var array = [];    
$(xml).find('organisationUnit ').each(function () {
    var ou = $(this).attr('id');
    array.push(ou); 
});

数组[0]现在将为g8cMOWx5ydN。

ou [0]的工作是将g8cMOWx5ydN转换为数组并返回索引0,即'g'。

编辑以下评论

您既可以在检索ID时使用ID,也可以不使用数组。

$(xml).find('organisationUnit ').each(function () {
    var id = $(this).attr('id');

    // use id ajax call here
});

或者您可以在每个

var array = [];    
$(xml).find('organisationUnit ').each(function () {
   var ou = $(this).attr('id');
   array.push(ou); 
});

//other code

for(var i=0; i<array.length; i++){
   var id = array[i];

   // use id ajax call here
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM