I am in the process of constructing a dashboard that automates logging into an API and updating specific data elements. I have successfully managed to login and authenticate, but I am unsure how to proceed with chaining the GET request after the POST action.
I've attempted to learn from various YouTube tutorials, creating functions and associating them with buttons and divs, but unfortunately, I am unable to display the retrieved data. The initial code segment executes the login process smoothly, but then it gets stuck and times out. I even tried making the login synchronous by calling a second open function, but it was unsuccessful.
<script type="text/javascript">
const xhr = new XMLHttpRequest();
var data = 'username=user&password=password';
xhr.onreadystatechange = function()
{
if (xhr.readyState == "4")
{
if (xhr.status == "200")
{
console.log(xhr.responseText);
}
if (xhr.status = "404")
{
console.log("FnF");
}
}
}
xhr.open('post','https://apiServer:8443/api/login', true)
xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded")
xhr.setRequestHeader("Accept", "application/xml")
//xhr.open('get', 'https://apiServer:8443/api/resource/items', true);
xhr.send();
I anticipate the login process to be executed discreetly in the background, while the GET request fetches the data to be displayed in a div element (I will refine the XML response once I have resolved the data retrieval issue).