Answers.Body: <p>The function contains this</p>
  
  <pre><code>if ($request-&gt;getAcceptsJson()) {
  </code></pre>
  
  <p>Thus you'll need to set the <code>dataType</code> in your ajax request to <code>json</code> or <code>text/json</code> in order to not get redirected. If you didn't set it correctly Craft will treat it as a normal post request instead of an <a href="http://api.jquery.com/jquery.ajax/" rel="noreferrer">ajax</a> request </p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-06-08T13:31:39.973Z
Answers.Text: Thank you so much! That fixed it. I had no idea CC2 was doing such a check.
Answers.UserDisplayName: Panos Spiliotis
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-06-08T16:29:39.333Z
Answers.Text: Nearly all Craft 3 controllers have this condition. So should keep it in mind
Answers.UserDisplayName: Robin Schambach
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-06-08T13:08:55.363Z
Answers.LastActivityDate: 2018-06-08T13:08:55.363Z
Answers.UserDisplayName: Robin Schambach
Body: <p>I'm working on a site that has recently migrated from Craft v2 with Commerce v1 to Craft v3 with Commerce v2. </p>
  
  <p>Using Commerce v1 I was able to update the cart via Ajax and get a data object with the state of the cart as a response. </p>
  
  <p>After updating to Craft Commerce v2 the ajax requests fail, returning <code>Status 302 - Redirect</code> instead of the data object, although the changes to the cart are successful. </p>
  
  <p>I can add one or more products or change their quantity via Ajax, however, I need the data object in order to properly update the state of the cart without reloading the page.</p>
  
  <ul>
  <li>I'm running Craft <code>v3.0.10.2</code> with Craft Commerce from <code>dev-develop</code> commit <code>1db58f6</code> on a machine running PHP <code>v7.1.12</code></li>
  <li>The action I'm using is <code>commerce/cart/update-cart</code></li>
  <li>The method of all requests is <code>POST</code></li>
  <li>Any form data should to be ok since the cart updates succesfully</li>
  <li>All requests are sent with the following headers:
  <code>
  Content-Type: application/x-www-form-urlencoded; charset=UTF-8
  X-Requested-With: XMLHttpRequest
  </code></li>
  </ul>
  
  <p>I'm I missing anything new in CC2? Any help would be much appreciated.</p>
  
Tags: <plugin-craftcommerce><plugin-craftcommerce2>
Title: 302 redirect when updating the cart via Ajax using Craft Commerce 2
ThreadText: # 302 redirect when updating the cart via Ajax using Craft Commerce 2
  
  (Asked by: username_0 on 2018-06-08)
  
  I'm working on a site that has recently migrated from Craft v2 with Commerce v1 to Craft v3 with Commerce v2.
  
  Using Commerce v1 I was able to update the cart via Ajax and get a data object with the state of the cart as a response.
  
  After updating to Craft Commerce v2 the ajax requests fail, returning `Status 302 - Redirect` instead of the data object, although the changes to the cart are successful.
  
  I can add one or more products or change their quantity via Ajax, however, I need the data object in order to properly update the state of the cart without reloading the page.
  
  * I'm running Craft `v3.0.10.2` with Craft Commerce from `dev-develop` commit `1db58f6` on a machine running PHP `v7.1.12`
  * The action I'm using is `commerce/cart/update-cart`
  * The method of all requests is `POST`
  * Any form data should to be ok since the cart updates succesfully
  * All requests are sent with the following headers:
    `Content-Type: application/x-www-form-urlencoded; charset=UTF-8
    X-Requested-With: XMLHttpRequest`
  
  I'm I missing anything new in CC2? Any help would be much appreciated.
  
  **Tags:** plugin-craftcommerce, plugin-craftcommerce2
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2018-06-08. Score: 5 | Accepted answer
  
  The function contains this
  
  ```
  if ($request->getAcceptsJson()) {
  
  ```
  
  Thus you'll need to set the `dataType` in your ajax request to `json` or `text/json` in order to not get redirected. If you didn't set it correctly Craft will treat it as a normal post request instead of an [ajax](http://api.jquery.com/jquery.ajax/) request
  
  > **Comments:**
  > **username_0**: Thank you so much! That fixed it. I had no idea CC2 was doing such a check.  
  > **username_1**: Nearly all Craft 3 controllers have this condition. So should keep it in mind  
  
  ---
  
  