问题描述
假设我有3个API,它们从应用程序返回XMLDocuments,而我只知道“服务器名称”,这是我的输入。
-
API 1:为我提供每个服务器的所有服务器名称和相关的唯一ID;
-
API 2:返回所有分配给服务器具有给定唯一ID的“模板”;并提供API 3的“即用型” URL,用于...
-
API 3:提供有关API 2中每个“模板”的每个详细信息;
因此,简而言之,我需要从API 1获取唯一ID,然后将其传递给API 2以获取URL,我需要从服务器的分配模板中获取所有详细信息。并且还考虑到我可能想从输入文本中用空格分隔的多个服务器中获取信息(和“多个”服务器”是我来这里寻求帮助的原因……大声笑)。我找不到一种处理.each循环的方法,以便获得包含我需要的所有信息的单个最终对象:
[
{
nodeId: nodeId,// node = server
nodeName: nodeName,aspectProperties: { ...objectWithAllAspectProperties },policyProperties: { ...ObjectWithAllPolicyProperties },},]
我真的在循环中迷路了。如果我使用分配了4个模板的服务器,并将console.log('final data')放在“ return data;”行之前。从main.js(请参见下文),控制台会多次返回数组,并随着循环的经过而递增。而且只有最后一个是我需要的:
[object Array][Array[1]]
[object Array][Array[1],Array[1]]
[object Array][Array[1],Array[1],Array[1]] //////// this is the one I need
我在编码方面是新手,所以请不要粗鲁地判断我的混乱代码...大声笑
所以...我写了4个js文件:main.js,getAllNodeIds.js,getNodeAssignmentList.js和getAssignmentDetailFromURL.js。
main.js:
import jQuery from "./vendor/jquery-3.5.1.min.js";
window.$ = window.jQuery = jQuery;
import { ajaxCall as getAllNodeIdsAjaxCall } from "./getFromApi/getAllNodeIds.js";
import { ajaxCall as getNodeAssignmentList } from "./getFromApi/getNodeAssignmentList.js";
import { ajaxCall as getAssignmentDetailFromURL } from "./getFromApi/getAssignmentDetailFromURL.js";
var filteredServersWithIds;
$("#getXml").click(function () {
filteredServersWithIds = getAllNodeIdsAjaxCall().then(function (data) {
var everyNodesAssignmentsAndPolicies = [];
$.each(data,function (i,jsonObj) {
$.each(jsonObj,function (key,val) {
getNodeAssignmentList(val.nodeId).then(function (data) {
$.each(data,jsonObj) {
getAssignmentDetailFromURL(jsonObj["detailedAssignmentURL"])
.then(function (data) {
everyNodesAssignmentsAndPolicies.push(data);
var everyNodesAssignmentsAndPoliciesWithoutEmptyObj = everyNodesAssignmentsAndPolicies.filter(
(value) => Object.keys(value).length !== 0
);
return everyNodesAssignmentsAndPoliciesWithoutEmptyObj;
})
.then(function (data) {
console.log("final data:");
console.log(data);
});
});
});
});
});
});
});
getAllNodeIds.js
/*
The output (return filteredServersWithIds) of the jQuery function ajaxCall below will be:
[
{ Hostname: "serverone",nodeId: "1" },{ Hostname: "servertwo",nodeId: "2" },{ ... }
]
*/
import jQuery,{ ajax } from "../vendor/jquery-3.5.1.min.js";
window.$ = window.jQuery = jQuery;
export function ajaxCall() {
// Defining AJAX settings
var ajaxURL = "http://localhost/api_1/getAllNodesWithIds";
var ajaxType = "GET";
var ajaxAcceptedResponseFormat = "text/xml";
var ajaxAcceptedResponseTxt = "text/plain";
var ajaxResponseParseMethod = "xml";
var ajaxContentType = "application/x-www-form-urlencoded; charset=UTF-8";
var ajaxAsynchronous = true;
var ajaxCache = false;
var ajaxCrossDomain = false;
var ajaxDataToTarget = null;
var ajaxUseGlobalHandlers = false;
var ajaxUsername = "admin";
var ajaxPassword = "1234";
var ajaxTimeout = 5000;
var haveRootNodeTag = "node_list";
var haveChildNodeTag = "nodes";
var haveExpectedFormat = 0;
var haveExpectedResponse = 0;
var haveNonExpected = "";
var haveRawResponse = 0;
var haveRawResponseData = "";
// Begin AJAX Work
var ajaxreq = $.ajax({
accepts: {
//xml: ajaxAcceptedResponseFormat,text: ajaxAcceptedResponseTxt,// Accepted response data
global: false,type: ajaxType,contentType: ajaxContentType,url: ajaxURL,beforeSend: function (xhr) {
xhr.setRequestHeader(
"Authorization","Basic " + btoa(ajaxUsername + ":" + ajaxPassword)
);
},async: ajaxAsynchronous,cache: ajaxCache,converters: { "text xml": jQuery.parseXML },crossDomain: ajaxCrossDomain,//,data: ajaxDataToTarget
dataType: ajaxResponseParseMethod,global: ajaxUseGlobalHandlers,ifModified: false,username: ajaxUsername,password: ajaxPassword,timeout: ajaxTimeout,})
.then(function (data) {
// splits multiple servers separated with spaces from input
var serversFromInput = splitServers();
var jsonOfNodesWithIds = [];
var filteredServersWithIds = [];
var returnedData;
// parses the XML response from the API 1
$(data)
.find("nodes node")
.each(function () {
var nodeId = $(this).find("node > id").text();
var nodeHostname = $(this).find("node > display_label").text();
jsonOfNodesWithIds.push({
nodeId: nodeId,Hostname: nodeHostname,});
});
$.each(serversFromInput,function (index,value) {
returnedData = $.grep(jsonOfNodesWithIds,function (element,index) {
return element.Hostname === value;
});
filteredServersWithIds.push(returnedData);
});
return filteredServersWithIds;
})
.fail(function (data) {
return (
"Loading XML data response failed with >> " +
data.statusText +
". Status code >> " +
data.statusCode
);
});
return ajaxreq;
}
export function splitServers() {
var nodeFilter = $("#serverFilter").val();
return nodeFilter.split(" ");
}
getNodeAssignmentList.js
/*
The output (return jsonOfNodeWithDetailedAspectURL) of the jQuery function ajaxCall below will be:
[
{
nodeId: "1",assignmentIsEnabled: true,detailedAssignmentURL: "http://localhost/api_2/getNodeAssignmentList/id/1",}
{
nodeId: "2",detailedAssignmentURL: "http://localhost/api_2/getNodeAssignmentList/id/2",}
{ ... }
]
*/
import jQuery,{ ajax } from "../vendor/jquery-3.5.1.min.js";
window.$ = window.jQuery = jQuery;
export function ajaxCall(nodeId) {
// Defining AJAX settings
var ajaxURL = "http://localhost/api_2/getNodeAssignmentList/id/" + nodeId;
var ajaxType = "GET";
var ajaxAcceptedResponseFormat = "text/xml";
var ajaxAcceptedResponseTxt = "text/plain";
var ajaxResponseParseMethod = "xml";
var ajaxContentType = "application/x-www-form-urlencoded; charset=UTF-8";
var ajaxAsynchronous = true;
var ajaxCache = false;
var ajaxCrossDomain = false;
var ajaxDataToTarget = null;
var ajaxUseGlobalHandlers = false;
var ajaxUsername = "admin";
var ajaxPassword = "1234";
var ajaxTimeout = 5000;
var haveExpectedFormat = 0;
var haveExpectedResponse = 0;
var haveNonExpected = "";
var haveRawResponse = 0;
var haveRawResponseData = "";
// Begin AJAX Work
var ajaxreq = $.ajax({
accepts: {
//xml: ajaxAcceptedResponseFormat,})
.then(
function (data) {
var jsonOfNodeWithDetailedAspectURL = [];
$(data)
.find("assignment_list template_version_assignment")
.each(function () {
var nodeId = $(this).find("related_ci > target_id").text();
var assignmentIsEnabled = $(this).find("is_enabled").text();
var unchangedDetailedAssignmentURL = $(this)
.find("link[rel=verbose]")
.attr("href");
var correctedDetailedAssignmentURL = unchangedDetailedAssignmentURL.replace(
/clusterapplication:80/,"localhost"
);
jsonOfNodeWithDetailedAspectURL.push({
nodeId: nodeId,assignmentIsEnabled: assignmentIsEnabled,detailedAssignmentURL: correctedDetailedAssignmentURL,});
});
$(data)
.find("assignment_list aspect_version_assignment")
.each(function () {
var nodeId = $(this).find("related_ci > target_id").text();
var assignmentIsEnabled = $(this).find("is_enabled").text();
var unchangedDetailedAssignmentURL = $(this)
.find("link[rel=verbose]")
.attr("href");
var correctedDetailedAssignmentURL = unchangedDetailedAssignmentURL.replace(
/clusterapplication:80/,});
});
return jsonOfNodeWithDetailedAspectURL;
} // End function
)
.fail(function (data) {
return (
"Loading XML data response failed with >> " +
data.statusText +
". Status code >> " +
data.statusCode
);
});
return ajaxreq;
}
getAssignmentDetailFromURL.js
import jQuery,{ ajax } from "../vendor/jquery-3.5.1.min.js";
window.$ = window.jQuery = jQuery;
export function ajaxCall(nodeDetailedAssignmentURL) {
// Defining AJAX settings
var ajaxURL = nodeDetailedAssignmentURL;
var ajaxType = "GET";
var ajaxAcceptedResponseFormat = "text/xml";
var ajaxAcceptedResponseTxt = "text/plain";
var ajaxResponseParseMethod = "xml";
var ajaxContentType = "application/x-www-form-urlencoded; charset=UTF-8";
var ajaxAsynchronous = true;
var ajaxCache = false;
var ajaxCrossDomain = false;
var ajaxDataToTarget = null;
var ajaxUseGlobalHandlers = false;
var ajaxUsername = "admin";
var ajaxPassword = "1234";
var ajaxTimeout = 5000;
var haveExpectedFormat = 0;
var haveExpectedResponse = 0;
var haveNonExpected = "";
var haveRawResponse = 0;
var haveRawResponseData = "";
// Begin AJAX Work
var ajaxreq = $.ajax({
accepts: {
//xml: ajaxAcceptedResponseFormat,})
.then(
function (data) {
var aspectProperties = [];
var policyProperties = [];
var objectOfNodeAspectAndPolicies = [];
$(data)
.find("aspect_version_assignment")
.each(function () {
var aspectIsEnabled = $(this).find("is_enabled").text();
var aspectTargetId = $(this)
.find("aspect_version_ref > target_id")
.text();
var aspectDisplayLabel = $(this)
.find("aspect_version_ref > display_label")
.text();
var aspectDescription = $(this)
.find("aspect_version_ref > description")
.text();
var aspectMajorVersion = $(this)
.find("aspect_version_ref > major_version")
.text();
var aspectMinorVersion = $(this)
.find("aspect_version_ref > minor_version")
.text();
var nodeId = $(this)
.find("related_ci > configuration_item > id")
.text();
var nodeType = $(this)
.find("related_ci > configuration_item > type")
.text();
var nodeName = $(this)
.find("related_ci > configuration_item > name")
.text();
aspectProperties.push({
nodeId: nodeId,nodeType: nodeType,nodeName: nodeName,aspectIsEnabled: aspectIsEnabled,aspectTargetId: aspectTargetId,aspectDisplayLabel: aspectDisplayLabel,aspectDescription: aspectDescription,aspectMajorVersion: aspectMajorVersion,aspectMinorVersion: aspectMinorVersion,});
$(data)
.find("parameter_value_list parameter_value")
.each(function () {
var policyType = $(this)
.find("parameter_context > defined_in_type")
.text();
var policyLabel = $(this)
.find("parameter_context > defined_in_label")
.text();
var policyDescription = $(this)
.find("parameter_context > defined_in_desc")
.text();
var metricId = $(this).find("parameter > id").text();
var metricDisplayLabel = $(this)
.find("parameter > display_label")
.text();
var metricType = $(this).find("parameter > type").text();
var metricDescription = $(this)
.find("parameter > description")
.text();
var metricDefaultValue = $(this)
.find("parameter > default_value > value")
.text();
var metricActualValue = $(this)
.find("parameter_value > value")
.text();
policyProperties.push({
policyType: policyType,policyLabel: policyLabel,policyDescription: policyDescription,metricId: metricId,metricDisplayLabel: metricDisplayLabel,metricType: metricType,metricDescription: metricDescription,metricDefaultValue: metricDefaultValue,metricActualValue: metricActualValue,});
});
objectOfNodeAspectAndPolicies.push({
nodeId: nodeId,aspectProperties: aspectProperties,policyProperties: policyProperties,});
});
return objectOfNodeAspectAndPolicies;
} // End function
)
.fail(function (data) {
return (
"Loading XML data response failed with >> " +
data.statusText +
". Status code >> " +
data.statusCode
);
});
return ajaxreq;
}
好吧...把它包装起来,这些js代码给了我我想要的东西,但是多次。当我追加数据时,这弄乱了我的html。
很抱歉,这个问题很大,但我想为您提供尽可能多的信息。
谢谢您,祝您编程愉快!
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)