waJsonController

Controller for processing AJAX requests and returning response as a JSON string

Contents...

Parent class: waController.

If you do not need to return response in the JSON format for AJAX requests, use class waController.

Main HTTP request processing logic must be described in public method execute().

Values returned to browser must be assigned to class field $response, without using json_encode() function, which is applied automatically.

To return error messages, assign them to class field $errors.

If there is a value set for the $errors field then the value of the status field in the JSON response is 'fail'. Otherwise it is 'ok'.

Methods

  • setError

    Sets a value for $errors field.

public function setError ($message, $data = array())

Sets a value for $errors field.

Parameters

  • $message

    Error message text.

  • $data

    Additional data associated with the error, which can be used by means of JavaScript in the browser.

Example

$this->setError(
    _wp('Cannot save settings. Some fields are empty.'),
    array($empty_fields)
);

//The above code is identical to this:
$this->errors[] = array(
    _wp('Cannot save settings. Some fields are empty.'),
    array($empty_fields),
);

Example

//Returning an error message without extra data
$this->setError(_wp('Cannot save settings. Some fields are empty.'));