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

    Arbitrary additional data associated with the error.

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