I'm trying to write some java script code that will (if the file exists) add its content to the web page, if it doesn't exist then it will check if the next file exists. I currently have it displaying the data if the file exists, however throws
GET http://localhost:8080/temp1.xml 404 (Not Found)
xmlhttp.onreadystatechange @ friends.html:56
XMLHttpRequest.send (async)
(anonymous) @ friends.html:95
if the file doesn't exist.
I've already tried adding i try catch around different parts but none of them seem to stop the error from occurring. ive also been trying to find a decent guide on how to handle not found errors but cant seem to find one that would work in my case.
this is the code I have without the try excepts.
xmlhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
JSObj = JSON.parse(this.responseText);
console.log(JSObj.friends.length);
for (i = 0; i < JSObj.friends.length; i++) {
name = JSObj.friends[i].name;
xhttp1.open("GET", "temp"+ i +".xml", true);
xhttp1.send();
};
};
};
this is the code I have with the try excepts
xmlhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
JSObj = JSON.parse(this.responseText);
console.log(JSObj.friends.length);
for (i = 0; i < JSObj.friends.length; i++) {
name = JSObj.friends[i].name;
try{
xhttp1.open("GET", "temp"+ i +".xml", true);
xhttp1.send();
}catch(err){
console.log(err);
};
};
};
};
the output I'm getting is a blank page due to the error stopping the code I have working from working which is when the error above is output. I expect that it would just skip that file and go onto the next one.