- ⌂ ajax
- Methods
ajax::error()
Send an AJAX response to report an error, optionally including a message, data and additional information.
Prototype
mixed error(mixed $message_or_data = '', array $options = [])
Parameters
- message_or_data - String containing error message, or associative array of arbitrary key/value pairs to send back with the error response.
- options - Optional associative array with the following keys.
- data - Associative array of key/value pairs to include in the response Data. If set_data() was call, these values will be added to that data. If keys overlap, the existing keys will be updated with these values.
- include_all - If TRUE (or message_or_data is a string), call ajax::include_all() internally to automatically provide additional metadata.
- "Result": "ERROR"
- "Timestamp": current timestamp
- "Message": one of the following values:
- If the first parameter is a string, it will be used.
- If ajax::set_meta_data() was called to set the message key/value, it will be used.
- Otherwise, "An error has occurred" will be used.
Return
Always returns FALSE, so an AJAX cMVC controller method can simply exit on an error with:
return $this->ajax->error('Error message here.');
Example
// return AJAX JSON response as if $ajax->include_all()
// was called, to produce:
// {
// "Result": "ERROR",
// "Timestamp": "2017-12-12 14:22:04",
// "Message": "Custom error.",
// }
$ajax = new \io_comand_web\ajax();
$ajax->error('Custom error');
// return AJAX JSON response that looks like:
// {
// "key": "value"
// }
$ajax = new \io_comand_web\ajax();
$ajax->error(['key'=>'value']);
// return AJAX JSON response as if $ajax->include_all()
// was called, to produce:
// {
// "Result": "ERROR",
// "Timestamp": "2017-12-12 14:22:04",
// "Message": "An error has occurred.",
// "Data": {
// "key": "value"
// }
// }
$ajax = new \io_comand_web\ajax();
$ajax->error(['key'=>'value'], ['include_all'=>TRUE]);
// return AJAX JSON response as if $ajax->include_all()
// was called, to produce:
// {
// "Result": "ERROR",
// "Timestamp": "2017-12-12 14:22:04",
// "Message": "Custom error.",
// "Data": {
// "key": "value"
// }
// }
$ajax = new \io_comand_web\ajax();
$ajax->set_data(['key'=>'value']);
$ajax->error('Custom error');