Javascript 使用jquery进行复杂的链式API调用,以从中获取信息
假设我有3个API从应用程序返回XMLDocuments,我只知道“服务器名称”,这是我的输入Javascript 使用jquery进行复杂的链式API调用,以从中获取信息,javascript,jquery,ajax,api,each,Javascript,Jquery,Ajax,Api,Each,假设我有3个API从应用程序返回XMLDocuments,我只知道“服务器名称”,这是我的输入 API 1:为每个服务器提供所有服务器名称和相关的唯一ID API 2:返回服务器分配给给定唯一ID的所有“模板”;还提供了API 3的“随时可用”URL,该URL API 3:提供API 2中每个“模板”的详细信息 因此,简而言之,我需要从API1获取唯一ID,并将其传递给API2,以获取从服务器分配的模板获取所有详细信息所需的URL。还考虑到我可能想从多个服务器获取信息,这些服务器之间用
- API 1:为每个服务器提供所有服务器名称和相关的唯一ID
- API 2:返回服务器分配给给定唯一ID的所有“模板”;还提供了API 3的“随时可用”URL,该URL
- API 3:提供API 2中每个“模板”的详细信息
[
{
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]]
[object Array][Array[1], Array[1]]
[object Array][Array[1], Array[1], Array[1]]
[object Array][Array[1], Array[1], Array[1], Array[1]] //////// this is the one I need
我在编码方面是新手,所以请不要粗鲁地评判我凌乱的代码…哈哈
所以…我写了4个js文件:main.js、getAllNodeIds.js、getNodeAsignmentList.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, function (i, 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(" ");
}
/*
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",
assignmentIsEnabled: true,
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,
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) {
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/,
"localhost"
);
jsonOfNodeWithDetailedAspectURL.push({
nodeId: nodeId,
assignmentIsEnabled: assignmentIsEnabled,
detailedAssignmentURL: correctedDetailedAssignmentURL,
});
});
return jsonOfNodeWithDetailedAspectURL;
} // End function
)
.fail(function (data) {
return (
"Loading XML data response failed with >> " +
data.statusText +
". Status code >> " +
data.statusCode
);
});
return ajaxreq;
}
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,
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) {
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,
nodeName: nodeName,
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;
}
getnodeasignmentlist.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(" ");
}
/*
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",
assignmentIsEnabled: true,
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,
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) {
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/,
"localhost"
);
jsonOfNodeWithDetailedAspectURL.push({
nodeId: nodeId,
assignmentIsEnabled: assignmentIsEnabled,
detailedAssignmentURL: correctedDetailedAssignmentURL,
});
});
return jsonOfNodeWithDetailedAspectURL;
} // End function
)
.fail(function (data) {
return (
"Loading XML data response failed with >> " +
data.statusText +
". Status code >> " +
data.statusCode
);
});
return ajaxreq;
}
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,
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) {
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,
nodeName: nodeName,
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;
}
getAssignmentDetailFromURL.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(" ");
}
/*
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",
assignmentIsEnabled: true,
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,
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) {
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/,
"localhost"
);
jsonOfNodeWithDetailedAspectURL.push({
nodeId: nodeId,
assignmentIsEnabled: assignmentIsEnabled,
detailedAssignmentURL: correctedDetailedAssignmentURL,
});
});
return jsonOfNodeWithDetailedAspectURL;
} // End function
)
.fail(function (data) {
return (
"Loading XML data response failed with >> " +
data.statusText +
". Status code >> " +
data.statusCode
);
});
return ajaxreq;
}
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,
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) {
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,
nodeName: nodeName,
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
很抱歉问这么大的问题,但我想给你尽可能多的信息
提前感谢您,祝您编码愉快!!!学习调试,分步调试,这样您就可以理解您的代码,尤其是在循环中使用调试消息来控制台了解正在发生的事情和时间。还要添加您收到的实际错误。不要盲目地期望这里的人会为您编写代码…这样您就不会我没有学到任何东西,几天/几个小时后你就会再次来到这里。这不是一个代码编写服务嗨!谢谢你的回复,伙计。所以,我试着在每次返回这些“.then”时都调试console.log,但我找不到其中哪一个只返回我需要的数组的最后输出,走出“.each”“声明。。。我真的迷路了(当然!我不是要一个新的代码来解决错误,伙计。我没有收到错误…这不是我在寻求帮助时说的。我得到了我需要的输出,但在多次中…这就是重点。谢谢!如果没有错误,那么按照我的建议在特定的位置放置console.log调试行,这样你就可以按照自己的建议进行操作了。)编写代码,并查看它的功能,以便您理解它。一旦您理解它,您将发现它为什么会执行多个输出:)