Cheking this right now: http://code4fun.io/post/laravel-ajax-register-and-login?utm_source=learninglaravel.net
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?
Please or to participate in this conversation.