Skip to content

Instantly share code, notes, and snippets.

@dgrigg
Created November 22, 2013 21:14
Show Gist options
  • Save dgrigg/7606996 to your computer and use it in GitHub Desktop.
Save dgrigg/7606996 to your computer and use it in GitHub Desktop.
A CodeIgniter helper function for returning JSON errors messages to AJAX calls instead of standard HTML error page. Use the helper instead of having to override the core Exception class.
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
function show_json_error($message, $status_code = 500, $status_message = '')
{
header('Cache-Control: no-cache, must-revalidate');
header('Content-type: application/json');
set_status_header($status_code, $status_message);
echo json_encode(
array(
'status' => FALSE,
'error' => 'Server Error',
'message' => $message
)
);
exit;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment