问题描述
试图创建一个将从API请求的XML文档中获取信息并将其放入2D数组中的脚本.
提出获取请求后 https://api.example.com/v1 .
为每个用户产生一个XML,看起来像
<User> <Id>Rdh9Rsi3k4U1</Id> <UserName>firstlast@email.com</UserName> <FirstName>First</FirstName> <LastName>Last</LastName> <Active>true</Active> <Email>firstlast@email.com</Email> <AccessLevel>Learner</AccessLevel> </User>
每个用户的外观相似的输出彼此堆叠在一起.如何将其擦到数组中?例如,第一个数组将具有7个"列",所有显示的信息都将有一个行. b
推荐答案
所以我为任何在将来寻求这种问题的答案的人弄清楚了.基本上,我发现我试图达到的API(如示例中所示,实际上不是" Citrowske.com")不允许使用CORS或JSONP,这使我拥有使用代理的唯一选择.
显示的代码的示例类似于我最终使用的代码(下图),以及所示的测试XML文件这里
关于其工作原理的基本说明,它使用代理来获取XML文件并将其存储为" XML",以"函数(XML)"为" XML".然后搜索XML DOC,并以"用户"开头的每个部分获取" firstName"和" lastName"数据从中删除并附加到名为" yourdropdownbox"的HTML部分中的下拉列表.
$.ajaxPrefilter( function (options) { if (options.crossDomain && jQuery.support.cors) { var http = (window.location.protocol === 'http:' ? 'http:' : 'https:'); options.url = http + '//cors-anywhere.herokuapp.com/' + options.url; //options.url = "http://cors.corsproxy.io/url=" + options.url; } }); $.get( 'http://citrowske.com/xml.xml', function (xml) { //console.log("> ", xml); //$("#viewer").html(xml); //////////////////////////////////// var select = $('#yourdropdownbox'); select.append('<option value="">Select a User</option>'); $(xml).find('User').each(function(){ var FirstNames = $(this).find('FirstName').text(); var LastNames = $(this).find('LastName').text(); select.append("<option value='"+ FirstNames +"'>"+FirstNames+" "+LastNames+"</option>"); }); } //////////////////////////////////// );
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script> <select id="yourdropdownbox"> </select>
注意,代理人以非常安全而闻名,因此请注意您使用它的方法.
另外,如果我想将数据转换为数组,而不是每次都可以添加
时附加数据var firstnamesarray = ["0"]; var lastnamesarry = ["0"]; var i = 0;
前行的前排斜线,然后更换:
var FirstNames = $(this).find('FirstName').text(); var LastNames = $(this).find('LastName').text();
firstnamesarry[i] = $(this).find('FirstName').text(); lastnamesarry[i] = $(this).find('LastName').text(); i = i+1;
并用
替换了" select.append"和姓氏firstnamearry[i] & lastnamearry[i]
要查看一个工作示例,请查看JSFIDDLE 在这里
问题描述
Attempting to create a script that will pull information from an API requested XML document and put it into a 2D array.
Upon making the Get request https://api.example.com/v1.svc/users?apikey=MY-KEY&source=MY-APP&limit=1000
An XML is produced for each user looking like
<User> <Id>Rdh9Rsi3k4U1</Id> <UserName>firstlast@email.com</UserName> <FirstName>First</FirstName> <LastName>Last</LastName> <Active>true</Active> <Email>firstlast@email.com</Email> <AccessLevel>Learner</AccessLevel> </User>
Each user has a similar looking output stacked on top of each other. How could this be scrubbed into an array? Example, the first array would have 7 "columns" with all shown information with each user having a row. b
推荐答案
So I figured it out for anyone looking for an answer to this type of question in the future. Basically, I found out that the API I was trying to reach (not actually "citrowske.com" as shown in the example) did not allow for CORS or jsonp which left me with the only option of using a Proxy.
Shown is an example of code similar to what I ended up using (below), along with the test XML file shown here
A basic explanation of how this works, it uses the proxy to get the XML file and stores it as "xml" found as "function(xml)". Then the XML doc is searched and each section that starts with "User" gets the "FirstName" and "LastName" data pulled from it and appended to dropdown in the HTML section named "yourdropdownbox".
$.ajaxPrefilter( function (options) { if (options.crossDomain && jQuery.support.cors) { var http = (window.location.protocol === 'http:' ? 'http:' : 'https:'); options.url = http + '//cors-anywhere.herokuapp.com/' + options.url; //options.url = "http://cors.corsproxy.io/url=" + options.url; } }); $.get( 'http://citrowske.com/xml.xml', function (xml) { //console.log("> ", xml); //$("#viewer").html(xml); //////////////////////////////////// var select = $('#yourdropdownbox'); select.append('<option value="">Select a User</option>'); $(xml).find('User').each(function(){ var FirstNames = $(this).find('FirstName').text(); var LastNames = $(this).find('LastName').text(); select.append("<option value='"+ FirstNames +"'>"+FirstNames+" "+LastNames+"</option>"); }); } //////////////////////////////////// );
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script> <select id="yourdropdownbox"> </select>
As a note, Proxy's are not known for being extremely secure, so watch out what you use this for.
Also, if I wanted to turn the data into an array instead of appending it each time I could have added
var firstnamesarray = ["0"]; var lastnamesarry = ["0"]; var i = 0;
Above the top row of forward-slashes and then replaced:
var FirstNames = $(this).find('FirstName').text(); var LastNames = $(this).find('LastName').text();
with
firstnamesarry[i] = $(this).find('FirstName').text(); lastnamesarry[i] = $(this).find('LastName').text(); i = i+1;
and replaced the "select.append" First & Last Names with
firstnamearry[i] & lastnamearry[i]
To view a working example, check out the jsfiddle here