I am currently utilizing a login form to generate and send a URL (referred to as the "full url" stored in the script below) that is expected to return a JSON object.
If the login details are accurate, the backend will respond with a JSON object containing a validation key for successful verification. (such as: [{"result":"VALID"}] )
In the case of incorrect login credentials, only a 500 error is provided.
The issue arises when encountering the 500 error; instead of taking action because the result is not "VALID," the script simply stops due to the lack of an object to validate.
How can I identify from the front end that a 500 error has been received and then trigger the function "bootThem()"?
function readTextFile(file, callback) {
var rawFile = new XMLHttpRequest();
rawFile.overrideMimeType("application/json");
rawFile.open("GET", file, true);
rawFile.onreadystatechange = function () {
if (rawFile.readyState === 4 && rawFile.status == "200") {
callback(rawFile.responseText);
}
}
rawFile.send(null);
}
window.onload = function () {
// obtain the URL from storage.
var fullURL = localStorage.getItem("fullURLStored")
// if the URL is empty, redirect back to the login page.
if(fullURL == ""){
bootThem();
}
// send the URL to the server.
readTextFile(fullURL, function (text) {
var data = JSON.parse(text);
if(data[0].result !== "VALID"){
bootThem();
} else{
//perform actions
}
});
}