waJsonController

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

Contents...

Parent class: waController.

If you do not need to return any response 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 any non-empty value for $errors field, then the JSON response contains status item equal to 'fail'. Otherwise status is equal to 'ok'.

Methods

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

Sets a value for $errors field.

Parameters

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.'));