简体   繁体   English

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

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

I want to use values from the web api , everything is running fine but when I read values from the url in a single variable it comes as large data: 我想使用来自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); }); } }); 

Now when I display ou it returns a list of different id's and I want to use all id's as separate value, can anybody tell me how to do that. 现在,当我显示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

When I use console.log(ou[0]); 当我使用console.log(ou [0]);时 it returns 1st char from each like: 它从每个类似的对象返回1个字符:

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

array[0] will now be g8cMOWx5ydN. 数组[0]现在将为g8cMOWx5ydN。

What ou[0] is doing is turning g8cMOWx5ydN into an array and returning index 0, which is 'g'. ou [0]的工作是将g8cMOWx5ydN转换为数组并返回索引0,即'g'。

Edit Following Comment 编辑以下评论

you can either use the Id as you retrieve it, and not use the array. 您既可以在检索ID时使用ID,也可以不使用数组。

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

    // use id ajax call here
});

or you can use the array after the each 或者您可以在每个

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.

相关问题 我想将 Excel 工作表中的数据样本存储到 javascript 中的数组中 - I want to store data samples from an excel sheet into an array in javascript 我希望能够存储和显示来自数组和本地存储内部的数据? - i want to be able to store and display data from an array and inside localstorage? 我想在没有 mvc 的 .net 中创建 Web api 来存储和检索 sql 中的数据 - I want to create Web api in .net without mvc to store and retrive data in sql 我想将图像从对象数组存储到<td> - I want to store an image from an array of objects into a <td> 我有一个API,我想从api响应中获取数据 - I have an API, I want to get the data from api response 我想从Extjs 4的商店中读取数据,但它显示[object object] - I want to read data from store for Extjs 4 but it shows [object object] Javascript:从网络提取CSV数据并存储在数组中 - Javascript: Pull csv data from web and store in array 我想在 React Native 中来自 API 的数组中添加一个值 - I want to add a value in array coming from API in React Native 如何从api获取数据并将其存储在mongodb模式数组中? - How to get data from an api and store it in an array of mongodb schema? 如何过滤来自 API 的数据并按年和月存储在数组中? - How to filter data coming from an API and store in an array by year and month?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM