How I can send a form with AJAX?

The problem is how I can make a user authentication with ajax. What is the correct way to send data and resivirlos on the controller.

I’m not sure if there’s really a “correct” way to do this, since it very much depends on the project context and the javascript framework you use.

The thing is that you’ll probably need to update your view based on how it changes for logged in users.

The authentification itself is not a problem at all, though. If you use jQuery, you can simply send an ajax request with auth data in post to the login method, i.e.

$.post('/login', $('#myform').serialize(), function(data) {
    ...
}, 'json');

But as I said, the tricky part is in using the returned data to update your view, which is why auth via ajax is often so awkward that a refresh is a much cleaner solution.

try making a URL redirect in the server if authentication is OK and return a response message if it fails, using the JavaScript to render the message .

You simply have to send the necessary data via json and then use a condition to decide what should happen, i.e.:

$.post('/login', $('#myform').serialize(), function(data) {
    if (data.status === 'success') {
        window.location.href = data.redirect;
    } else {
        $.toast(data.error_message, 10000, 'error');
    }
}, 'json');

You can also add the validation errors via $model->errors() to the json.

Thank you, it was of great help.