I'm somewhat new to web scripting. I'm used to the Java try-catch style, and the Javascript ones seem to be similar. However, when I try to use it, the results I get are not quite the same. This is my try-catch block:
try {
loadAllInfo(<?php echo get_students_from_group();?>);
} catch (exception) {
alert("Invalid data entered, please try again.");
window.location.href = "studentDisplayFromService.php";
} finally {
setHTMLStudentTable("all");
}
The objective is that if the PHP request fails (the input needs to have a very specific syntax, but in this case, it is not possible to check it beforehand), this takes the user back to the input website, since the PHP request sends back something that cannot be parsed to JSON. My code works perfectly when the syntax is right, but when it's not, instead of catching the exception, it appears on the console instead:
SyntaxError: expected expression, got '<' (And it shows the line where the problem is, obviously, this one.)
I have tried throwing an exception myself inside the try block like...
try {
loadAllInfo(<?php echo get_students_from_group();?>);
throw new exception("Error");
}
... and it captures it in this case, shows the alert and takes the user back to the origin website.
Any idea of what could be causing this issue or how to solve it? I haven't been able to find any similar questions here :/
Thank you very much in advance!
echo json_encode($arr)you shouldn't have to JSON.parse it. The output won't have quotes around it to make it a string so it will already represent a javascript array or object in javascript compiler