<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>

CC BY-SA 3.0
2014-10-10T21:54:50.057Z
2014-10-10T21:54:50.057Z
Brandon Kelly
<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>

CC BY-SA 4.0
2020-09-30T13:09:52.067Z
2020-09-30T13:09:52.067Z
Duško Šobić
<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>

<ajax><login>
Front end login form with ajax
# 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/keithmancuso/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);
            }
        });
 });


---

