Welcome Guest, Not a member yet? Register   Sign In
Handling errors differently when calling from Ajax
#1

[eluser]Fumigator[/eluser]
I'd like to handle errors with a different template when calling from Ajax (with JSON as the return type) but I don't quite know what class I need to extend to make this change.

Currently I'm using the normal controller and model classes when calling via Ajax, and then inside a method in, say, the model class:

Code:
class M_accounts extends CI_Model {
function updateBankBalance($accountId, $amt)
    {
  $this->load->database();
  
  $query = $this->db->query("
    UPDATE t_accounts
    SET  bank_balance_am = $amt
    WHERE account_id = $accountId
  ");
  
  $updateCount = $this->db->affected_rows();
  return $updateCount;
}

If this query produces an error, it's buried in the Ajax response and is not JSON formatted so my Javascript doesn't know what to do with it.

I've isolated the spot in the CI core code I'd like to change, it's in system\database\DB_driver.php in the display_error() function. Instead of
Code:
echo $error->show_error($heading, $message, 'error_db');
I want to do
Code:
echo $error->show_error($heading, $message, 'error_ajax', 200);
With that error_ajax being a template I created that outputs a json encoded error.

I'm just not sure what class to extend to provide for this different output. I don't want to change the actual DB_driver.php, that'll break normal non-ajax errors, and I'm sure it's the wrong way to do it.

Can anyone point me in the right direction?


Messages In This Thread
Handling errors differently when calling from Ajax - by El Forum - 12-27-2013, 01:21 PM
Handling errors differently when calling from Ajax - by El Forum - 12-27-2013, 02:05 PM
Handling errors differently when calling from Ajax - by El Forum - 12-27-2013, 02:41 PM
Handling errors differently when calling from Ajax - by El Forum - 12-27-2013, 03:58 PM



Theme © iAndrew 2016 - Forum software by © MyBB