0

Basically, I was sending a AJAX request with an object with some missing JSON data, and I was wondering why I didn't get a status code and got no response data. The JSON is valid, but there are missing data that won't allow the code to successfully run.

So I tried it several times and the request never returned any data and there was no status code as if the server didn't respond and the request timed out. I tried with a correct JSON and it worked flawlessly (I got a 200 status and a proper server response)

public function getAssets($asset_id)   {  
    try {
        $response = $this->_services[$current_service]->get_random_asset($asset_id);
    } catch (Exception $e) {
        throw new Exception('Something is wrong: '.$e->getMessage(), 500);
    }

}

I am not sure if this is correct, but I am guessing if I don't catch the thrown Exception by putting the function call of getAssets() in a try block or any function higher in the stack the server will not send any response and the request will timeout?

aLex
  • 1
  • 11
  • No! PHP Will crash with something like `Fatal error: Uncaught Exception: Something is wrong: in \PHP-SOURCE\tst.php on line xxx` – RiggsFolly May 24 '19 at 17:02
  • If an exception is thrown and no one catches it, the script gets a fatal error and exits. – Barmar May 24 '19 at 17:02
  • Use the Network tab of the browser console, you'll probably see that error message. Since it's not valid JSON, the client application will not be able to process it. – Barmar May 24 '19 at 17:04
  • It sounds like you probably have display_errors turned off. You won't be able to see the error unless display_errors is enabled; PHP won't output anything if there's a fatal error. As far as the HTTP code, you probably won't get a 500 even if there's a fatal error. See https://stackoverflow.com/questions/28972933/apache-php-returns-http-status-code-200-on-error-pages – Don't Panic May 24 '19 at 18:00
  • If display_errors is turned off, I won't get a status code in the network tab? – aLex May 24 '19 at 18:38

0 Answers0