0

I have an AJAX function that gets an error whenever it gets a JSON response from my other webpage that has an error printed at the end of the JSON response. When there is no error, the function reports normally, but I don't know how to get the AJAX function to ignore the error and just read the JSON part of the response.

HTML:

$.ajax({
                url: 'https://example.com/quickstart_data.php',
                type: 'POST',
                dataType: 'json',
                cache: false,
                success: function(result) {
                    $("#timeToLeave").html(result['alarm']['timeToLeave']),
                    $("#timeToLeave").css({'color':result['background']['text']});
                }

But the website responds something like this:

    {"background":{"fill":"#202124","text":"White"},"event":{"unixTime":1607040000,"date":"4-12-20","day":"Fri","time":"10:30","dayFinish":"Fri","timeFinish":"11:30"}
Fatal error: Uncaught RuntimeException: Unable to find a speaker for this alarm in /var/www/html/vendor/duncan3dc/sonos/src/Alarm.php:128 Stack trace: #0 /var/www/html/vendor/duncan3dc/sonos/src/Alarm.php(74): duncan3dc\Sonos\Alarm->getSpeaker() #1 /var/www/html/vendor/duncan3dc/sonos/src/Alarm.php(660): duncan3dc\Sonos\Alarm->soap('AlarmClock', 'UpdateAlarm', Array) #2 /var/www/html/vendor/duncan3dc/sonos/src/Alarm.php(167): duncan3dc\Sonos\Alarm->save() #3 /var/www/GoogleAlarm/sonoscontroller.php(47): duncan3dc\Sonos\Alarm->setTime(Object(duncan3dc\Sonos\Utils\Time)) #4 /var/www/GoogleAlarm/quickstart_data.php(362): sonosAlarmUpdate('14', '08:54', false, '10') #5 {main} thrown in /var/www/html/vendor/duncan3dc/sonos/src/Alarm.php on line 128

**note I have removed irrelevant data in the JSON return because it is irrelevant to the problem

The "Fatal error:" part is not normally there unless there is a different error, however I still want the AJAX get JSON response to work. How do I do this? I do not want to turn it off in PHP.ini

  • You can use [set_error_handler()](https://stackoverflow.com/questions/277224/how-do-i-catch-a-php-fatal-e-error-error) in `alarm.php` to prevent that fatal error from being output to Response or you can also use the same function to introduce an `error` key in the JSON whose value explains the error. – cstayyab Dec 02 '20 at 05:19

1 Answers1

2

If you really want to keep the error in the result parameter of success() but ignore it, do this before accessing the values of result :

let fatalErrorIndex = result.indexOf("Fatal error:");
if(fatalErrorIndex != -1) result = result.substring(0, fatalErrorIndex);
result = JSON.parse(result.trim());

Also, set the dataType to 'text' as you are manually parsing the JSON this way.

However, you should consider implementing ajax error handling. This way if anything wrong happens on the PHP side you'll be notified by the AJAX error callback:

$.ajax({
    url: 'https://example.com/quickstart_data.php',
    type: 'POST',
    dataType: 'json',
    cache: false,
    success: function(result) {
    },
    error: function (xhr, ajaxOptions, thrownError) {
        // Handle errors here
    }
}

This is the correct way to handle errors. See the documentation for more info

KonyTech
  • 35
  • 10