Adds initial ajax call to agents api

main
Yasen Pramatarov 2024-09-27 09:49:50 +03:00
parent e5d1324126
commit ede9ecc7b6
3 changed files with 27 additions and 1 deletions

View File

@ -6,7 +6,10 @@
<?php foreach ($agentDetails as $agent) { ?>
<p class="card-text">
agent id<?= htmlspecialchars($agent['id']) ?>: type <?= htmlspecialchars($agent['agent_type_id']) ?>, url <?= htmlspecialchars($agent['url']) ?>
<button onclick="fetchData()">fetch data</button>
<button onclick="fetchData(true)">force refresh</button>
</p>
<p>Result:</p>
<pre id="result">click a button to fetch data from the agent.</pre>
<?php } ?>

View File

@ -23,6 +23,9 @@
}
})();
</script>
<?php if ($page === 'agents') { ?>
<script src="<?= $app_root ?>static/agents.js"></script>
<?php } ?>
<title>Jilo Web</title>
<link rel="icon" type="image/x-icon" href="<?= $app_root ?>static/favicon.ico">
</head>

View File

@ -0,0 +1,20 @@
function fetchData(force = false) {
// Show loading text
document.getElementById("result").innerHTML = "Loading...";
// Create an AJAX request
var xhr = new XMLHttpRequest();
xhr.open("POST", "index.php?page=agents", true);
xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
xhr.onreadystatechange = function() {
if (xhr.readyState === 4 && xhr.status === 200) {
// Parse and display the result
let result = JSON.parse(xhr.responseText);
document.getElementById("result").innerHTML = JSON.stringify(result, null, 2);
}
};
// Send the AJAX request, with force flag
xhr.send("action=fetch&force=" + (force ? 'true' : 'false'));
}