webCOMAND

ajax::ok()

Send an AJAX response to report success, optionally including data and additional information.

Prototype

boolean ok(mixed $message_or_data = '', array $options = [])

Parameters

  • message_or_data - String containing message, or associative array of arbitrary key/value pairs to send back with the 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": "OK"
      • "Timestamp": current timestamp

Return

Always returns TRUE, so an AJAX cMVC controller method can simply exit with:
return $this->ajax->ok();

Example

// return AJAX JSON response that looks like:
// {
//     "key": "value"
// }
$ajax = new \io_comand_web\ajax();
$ajax->ok(['key'=>'value']);

// return AJAX JSON response as if $ajax->include_all()
// was called, to produce:
// {
//     "Result": "OK",
//     "Timestamp": "2017-12-12 14:22:04",
//     "Message": "Completed Task."
// }
$ajax = new \io_comand_web\ajax();
$ajax->ok('Completed Task.');

// return AJAX JSON response like:
// {
//     "Result": "OK",
//     "Timestamp": "2017-12-12 14:22:04",
//     "Data": {
//         "key1": "value1",
//         "key2": "value2"
//     }
// }
$ajax = new \io_comand_web\ajax();
$ajax->ok(['key1'=>'value1', 'key2'=>'value2'], ['include_all'=>TRUE]);

// return AJAX JSON response like:
// {
//     "Result": "OK",
//     "Timestamp": "2017-12-12 14:22:04",
//     "Message": "Completed Task." 
//     "Data": {
//         "key1": "value1",
//         "key2": "value2"
//     }
// }
$ajax = new \io_comand_web\ajax();
$ajax->set_meta_data('Message', 'Completed Task.');
$ajax->ok(['key1'=>'value1', 'key2'=>'value2'], ['include_all'=>TRUE]); 

// return AJAX JSON response like:
// {
//     "Result": "OK",
//     "Timestamp": "2017-12-12 14:22:04",
//     "Message": "Completed Task." 
//     "Data": {
//         "key1": "value1",
//         "key2": "value2"
//     }
// }
$ajax = new \io_comand_web\ajax();
$ajax->set_all(['key1'=>'value1', 'key2'=>'value2']);
$ajax->ok('Completed Task.', ['include_all'=>TRUE]);

// return AJAX JSON response as if $ajax->include_all()
// was called, to produce:
// {
//     "key1": "value1"
//     "key2": "value2"
// }
$ajax = new \io_comand_web\ajax();
$ajax->ok(['key1'=>'value1', 'key2'=>'value2']);

Related

error()