Answers.Body: <p>If you’re getting the <code>{success: true}</code> response, then the user is getting logged in successfully, and the cookie would have actually been created by that response.</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-10-10T21:54:50.057Z
Answers.LastActivityDate: 2014-10-10T21:54:50.057Z
Answers.UserDisplayName: Brandon Kelly
Answers.Body: <p>I had a same problem and I will try to give you more detailed explanation.</p>
  <p>Craft thinks you submitted a classic login form and Users controller tries to redirect you after successful login. In case you send AJAX request Craft will return success:true. So, you should add dataType: &quot;json&quot; in your AJAX request and you will be logged in with response success:true.</p>
  <p>All in all, your code should be:</p>
  <pre><code>$('#loginForm').on('submit', function(e) {
          e.preventDefault();
          var formData = $(this).serialize();
  
          $.ajax({
              url: '/',
              contentType: &quot;application/x-www-form-urlencoded&quot;,
              dataType: &quot;json&quot;,
              type:'POST',
              data: formData,
              success: function(result) {
                  console.log(result);
              }
          });
   });
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2020-09-30T13:09:52.067Z
Answers.LastActivityDate: 2020-09-30T13:09:52.067Z
Answers.UserDisplayName: Duško Šobić
Body: <p>Im trying to get a front end login form to work over ajax and running into issues, it seems to always pass back success and not sure how to actual make sure the cookie gets created and the user is logged in?</p>
  
  <p>here is the gist of what i have right now:
  <a href="https://gist.github.com/keithmancuso/29e8446ec5fa66fb2b01" rel="nofollow">https://gist.github.com/keithmancuso/29e8446ec5fa66fb2b01</a></p>
  
Tags: <ajax><login>
Title: Front end login form with ajax
ThreadText: # Front end login form with ajax
  
  (Asked by: username_0 on 2014-10-10)
  
  Im trying to get a front end login form to work over ajax and running into issues, it seems to always pass back success and not sure how to actual make sure the cookie gets created and the user is logged in?
  
  here is the gist of what i have right now:
  <https://gist.github.com/username_0/29e8446ec5fa66fb2b01>
  
  **Tags:** ajax, login
  
  
  ---
  
  ## 2 answers
  
  ### Answer by username_1 on 2014-10-10. Score: 3
  
  If you’re getting the `{success: true}` response, then the user is getting logged in successfully, and the cookie would have actually been created by that response.
  
  
  ---
  
  ### Answer by username_2 on 2020-09-30. Score: 2
  
  I had a same problem and I will try to give you more detailed explanation.
  
  Craft thinks you submitted a classic login form and Users controller tries to redirect you after successful login. In case you send AJAX request Craft will return success:true. So, you should add dataType: "json" in your AJAX request and you will be logged in with response success:true.
  
  All in all, your code should be:
  
  ```
  $('#loginForm').on('submit', function(e) {
          e.preventDefault();
          var formData = $(this).serialize();
  
          $.ajax({
              url: '/',
              contentType: "application/x-www-form-urlencoded",
              dataType: "json",
              type:'POST',
              data: formData,
              success: function(result) {
                  console.log(result);
              }
          });
   });
  
  ```
  
  
  ---
  
  