EducateYourself
EducateYourself

Reputation: 979

ajax call error handling function or if-else statement

I would like to know if it is necessary to use ajax call error handling function in the below mentioned example if I already used "if-else" statement:

My model:

 if ($query && $query->num_rows() > 0) {

        return $query->result();
    } else {
        return false;
    }    

My controller:

if ($this->model_users->did_get_data($user_id)) {

            $data["results"]= $this->model_users->did_get_data($user_id);           
            echo json_encode($data);            
        }
        else {      
        $data = array(
                    'message' => "Could not find any posts."
                     );
            echo json_encode($data);

        }

My JS file:

$.get('controller/get_data', function (data) {
        if (data.message !== undefined) {
            $( "#data_is_not_got").text(data.message);
        } else {

           //displaying posts               

        }
    }, "json");

Upvotes: 0

Views: 491

Answers (2)

hffmr
hffmr

Reputation: 128

This depends on what can happen to your controller.

If you are absolutely sure there can't be any 404 error or there won't be any timeout error, you can work without the error handling function. But because I'm sure you can't say it's 100% sure, I would say you have to implement it ;)

But here is my tip: if you use AJAX calls more than once, create your own "AJAX API" which only uses one callback where you can use a status parameter to tell the callback whether everything is ok or not, so in fact you only have to use one if-else statement.

This is a short example from one of my methods to deal with a PHP-backend:

ajax = function (callback) {
    $.ajax({
        // whatever you need here
        success: function (response) {
            if (typeof callback == "function") {
                callback(true, response);
            }
        },
        error: function (e) {
            if (typeof callback == "function") {
                if (e.statusText == "timeout") {
                    callback(false, {"timeout": true})
                } else {
                    try {
                        JSON.parse(e.responseText)

                        // if try is successfull, there is no fatal-error (which would exit the PHP, so no JSON)
                        callback(false, {})
                    } catch (x) {
                        // only maximum-execution-time-errors are fatal errors :P
                        if (e.responseText.indexOf('<b>Fatal error</b>') != -1) {
                            callback(false, {"timeout": true})
                        }
                    }
                }
            }
        },
    })
}

Upvotes: 1

dmccabe
dmccabe

Reputation: 1034

Yes, you should still add an error handler to the AJAX call. That will let you gracefully handle any number of potential issues that could occur with the call that your if/else statement doesn't cover - can't connect to server, internal server error, etc.

Upvotes: 1

Related Questions