I want to call an action via an ajax call with the following code:

    url: Craft.getActionUrl(_this.options.updateRestockAction),
    method: 'POST',
    headers: headers,
    data: data,
    success: function(data) {
    error: function(jqXHR, textStatus) {
    complete: function(jqXHR, textStatus) {

My action is being called successfully. However, I can't find out how to return data to the success callback. Any idea how to do this? I'm using Craft 3.


  • Do you return an object of type Response in your controller? Apr 10, 2018 at 13:28

1 Answer 1


As Robin mentions, your controller needs to return an object of type Response, and in your case you probably want to return it with the format set to json. There's a helper method in Craft's Controller class, asJson, which is handy for this. Example:

public function actionMyControllerAction()
    $result = ['my' => 'data'];
    return $this->asJson($result);

This should give you back an object for data with a key of my with value data.

  • Thanks, this worked! Along with correcting a small typo "success: function" instead of "succes: function" Apr 10, 2018 at 14:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.