Answers.Body: <p>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 <code>Controller</code> class, <code>asJson</code>, which is handy for this. Example:</p>
  
  <pre><code>public function actionMyControllerAction()
  {
      $result = ['my' =&gt; 'data'];
      return $this-&gt;asJson($result);
  }
  </code></pre>
  
  <p>This should give you back an object for <code>data</code> with a key of <code>my</code> with value <code>data</code>.</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2018-04-10T14:12:08.180Z
Answers.Text: Thanks, this worked! Along with correcting a small typo "success: function" instead of "succes: function"
Answers.UserDisplayName: offtheshore
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2018-04-10T13:51:19.817Z
Answers.LastActivityDate: 2018-04-10T13:51:19.817Z
Answers.UserDisplayName: André Elvan
Body: <p>I want to call an action via an ajax call with the following code:</p>
  
  <pre><code>$.ajax({
      url: Craft.getActionUrl(_this.options.updateRestockAction),
      method: 'POST',
      headers: headers,
      data: data,
      success: function(data) {
          console.log(data);
      },
      error: function(jqXHR, textStatus) {
          console.log('error');
      },
      complete: function(jqXHR, textStatus) {
          console.log('complete');
      }
  });
  </code></pre>
  
  <p>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.</p>
  
  <p>Thanks</p>
  
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2018-04-10T13:28:46.277Z
Comments.Text: Do you return an object of type `Response` in your controller?
Comments.UserDisplayName: Robin Schambach
Tags: <plugin-development><craft3>
Title: How to return data to an ajax succes callback
ThreadText: # How to return data to an ajax succes callback
  
  (Asked by: username_0 on 2018-04-10)
  
  I want to call an action via an ajax call with the following code:
  
  ```
  $.ajax({
      url: Craft.getActionUrl(_this.options.updateRestockAction),
      method: 'POST',
      headers: headers,
      data: data,
      success: function(data) {
          console.log(data);
      },
      error: function(jqXHR, textStatus) {
          console.log('error');
      },
      complete: function(jqXHR, textStatus) {
          console.log('complete');
      }
  });
  
  ```
  
  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.
  
  Thanks
  
  **Tags:** plugin-development, craft3
  
  > **Comments:**
  > **username_1**: Do you return an object of type `Response` in your controller?  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_2 on 2018-04-10. Score: 2 | Accepted answer
  
  As username_1 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`.
  
  > **Comments:**
  > **username_0**: Thanks, this worked! Along with correcting a small typo "success: function" instead of "succes: function"  
  
  ---
  
  