Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

sstevan's avatar

Laravel 5.3 login using AJAX doesn't redirect

Hello,

I'm trying to make AJAX login using Laravel 5.3 Auth.

Here's what I got so far:

var login = function()
{
var data = {};

data["email"] = $('#email').val();
data["password"] = $('#password').val();

if($('#remember').is(':checked'))
    data["remember"] = "on";

$.ajax({
    type: "POST",
    url: '/login',
    data: JSON.stringify(data),
    // data: data,
    headers : { 'Content-Type': 'application/json' },

    success: function(data) {
        console.log(data);
        // window.location.href = "/dashboard";
    }
});
};

I'm sending CRSF token as X-CSRF-TOKEN header.

The problem is that when I successfully login, I say on the same page, but in Network tab I can see that /dashboard page is loaded by I'm not redirected.

In the same manner, when I pass wrong credentials, I stay on the same page, but I can see that /login page is loaded in the separate call with an error message that should be actually displayed.

Also, I've tried without headers : { 'Content-Type': 'application/json' }, and sending data as: data = data, but I get the same thing.

Why the browser doesn't redirect to that page since it is loading it in the "background"?

I'm getting correct page as request response as well, I can see it in console (console.log(data);).

I'd love to make this working using Laravel Auth features without overriding. Is it possible?

0 likes
2 replies
rdelorier's avatar

You should disable redirects with the accept header 'Accept': 'application/json' then handle the response yourself otherwise its kinda pointless to use ajax.

Please or to participate in this conversation.