Answers.Body: <p>Controllers in Craft 4 generally use the <a href="https://github.com/craftcms/cms/blob/2eac9249964ccc553bf841c79b9ee44d58f16b61/src/web/Controller.php#L244-L272" rel="nofollow noreferrer">asSuccess</a> and <a href="https://github.com/craftcms/cms/blob/2eac9249964ccc553bf841c79b9ee44d58f16b61/src/web/Controller.php#L214-L242" rel="nofollow noreferrer">asFailure</a> methods, which return <code>200</code> and <code>400</code> status codes respectively, in response to JSON requests.</p>
  <p>Both methods also send a <code>message</code> parameter in their response, in case there's something to report, so the following should work.</p>
  <pre class="lang-javascript prettyprint-override"><code>$.ajax({...})
      .done(function(data) {
          console.log('success: ' + data.message);
      })
      .fail(function(data) {
          console.log('fail: ' + data.message);
      });
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2022-06-24T16:00:50.743Z
Answers.Text: This worked, thank ya sir!
Answers.UserDisplayName: Mike Niland
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2022-06-24T15:41:30.780Z
Answers.LastActivityDate: 2022-06-24T15:41:30.780Z
Answers.UserDisplayName: Ben Croker
Body: <p>I have a project that was initially built in Craft 3 and I'm upgrading it to Craft 4. I leverage ajax on one of the pages to save data to a user in the CMS. The following code worked in Craft 3 but no longer works in Craft 4. The ajax request submits correctly and the data is saved to the CMS but <code>data.success</code> no longer returns true. What's the ideal way to handle an ajax pass/fail in Craft 4?</p>
  <pre><code>$.ajax({
      contentType: false,
      processData: false,
      method: &quot;POST&quot;,
      url: '',
      dataType: &quot;json&quot;,
      data: formData
  }).done(function(data) {
      if (data.success == true) {
          console.log(data, 'success')
      } else {
          console.log(data, 'fail')
      }
  }).fail(function(data) {
      console.log(data, 'fail')
  });
  </code></pre>
  
Tags: <form><ajax><craft4>
Title: Ajax response no longer returns true after updating to craft 4
ThreadText: # Ajax response no longer returns true after updating to craft 4
  
  (Asked by: username_0 on 2022-06-24)
  
  I have a project that was initially built in Craft 3 and I'm upgrading it to Craft 4. I leverage ajax on one of the pages to save data to a user in the CMS. The following code worked in Craft 3 but no longer works in Craft 4. The ajax request submits correctly and the data is saved to the CMS but `data.success` no longer returns true. What's the ideal way to handle an ajax pass/fail in Craft 4?
  
  ```
  $.ajax({
      contentType: false,
      processData: false,
      method: "POST",
      url: '',
      dataType: "json",
      data: formData
  }).done(function(data) {
      if (data.success == true) {
          console.log(data, 'success')
      } else {
          console.log(data, 'fail')
      }
  }).fail(function(data) {
      console.log(data, 'fail')
  });
  
  ```
  
  **Tags:** form, ajax, craft4
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2022-06-24. Score: 1 | Accepted answer
  
  Controllers in Craft 4 generally use the [asSuccess](https://github.com/craftcms/cms/blob/2eac9249964ccc553bf841c79b9ee44d58f16b61/src/web/Controller.php#L244-L272) and [asFailure](https://github.com/craftcms/cms/blob/2eac9249964ccc553bf841c79b9ee44d58f16b61/src/web/Controller.php#L214-L242) methods, which return `200` and `400` status codes respectively, in response to JSON requests.
  
  Both methods also send a `message` parameter in their response, in case there's something to report, so the following should work.
  
  ```javascript
  $.ajax({...})
      .done(function(data) {
          console.log('success: ' + data.message);
      })
      .fail(function(data) {
          console.log('fail: ' + data.message);
      });
  
  ```
  
  > **Comments:**
  > **username_0**: This worked, thank ya sir!  
  
  ---
  
  