1

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!

11
  • When you 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 Commented Dec 16, 2017 at 0:56
  • Thank you, but I tried without it too, and the problem persists anyway :/ Commented Dec 16, 2017 at 0:58
  • Look at the page source code. Suspect it isn't printing exactly what you expect. Or the problem isn't in the code shown Commented Dec 16, 2017 at 0:58
  • Or perhaps this code doesn't exist in a php file and you are trying to run it in a js file Commented Dec 16, 2017 at 1:00
  • 1
    Ok. I thought so. Make a cURL request instead. You can access the remote status and content-type header which in the case of the error shown in comment below would be a 500 server error status and if it's json will have a application/json content type (at least it should). A good request library to use would be Guzzle Commented Dec 16, 2017 at 1:18

1 Answer 1

1

You need to write it like this:

JSON.parse('<?php echo json_encode(get_students_from_group());?>')

Some examples and detailed descriptions can be found here: JSON.parse

If you get bad stuff as result you might have to check if its valid in your loadAllInfo() function.

Better catch that error in your PHP function get_students_from_group() before trying to do something in Js with it. As you mentioned in the comments you get a 400 status code as response. So try to catch that. Successful calls will usually return a 200 http status code.

If you need to use file_get_contents() you can check the $http_response_header.

Better use cUrl for more control:

<?php
$ch = curl_init('http://...');
curl_exec($ch);
if (!curl_errno($ch)) {
    $info = curl_getinfo($ch); // ... Process information
}
curl_close($ch);
?>
Sign up to request clarification or add additional context in comments.

8 Comments

I removed the JSON parse from the question in an edit, I'm afraid that's not the solution, the '<' thing it returns is a chunk of HTML obtained from the service the PHP gets.
What it returns looks something like this: <br /> <b>Warning</b>: file_get_contents(homepages.herts.ac.uk/%7Ecomqgrs/ads/…): failed to open stream: HTTP request failed! HTTP/1.1 400 Bad Request in <b>/Applications/XAMPP/xamppfiles/htdocs/ADS/coursework/showStudentList.php</b> on line <b>11</b><br />
@DanielG you need to catch that error long before the steps in the question
Can you show what your loadAllInfo() looks like please?
Sure: function loadAllInfo(moduleObject) { loadModuleInfo(moduleObject); loadStudentInfo(moduleObject); loadGroupInfo(moduleObject); } function loadModuleInfo(moduleObject) { moduleCode = moduleObject["code"]; moduleName = moduleObject["name"]; moduleCohort = moduleObject["cohort"]; }
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.