简体   繁体   中英

User login using modal that stays open on error

I am trying to use a modal for user login. Logging in works but the form just closes when I type in a wrong password. The form is pretty basic with an id of "idForm." Below is my JQuery:

$(document).ready(function(){

      $("#idForm").submit(function(e) {

      $.ajax({
            type: "POST",
            url: "{{ url('/auth/login') }}",
            data: $("#idForm").serialize(),
            success: function(data)
            {
                location.reload();
            }
          });

      e.preventDefault(); 
  });

});

Based on Laravel 5.1, I know if there is a problem, it sends $errors back to the same page but I don't know how to retrieve it within the same modal using Ajax.

Any help would be greatly appreciated. Thanks.

the success function executes when there has been a successful ajax call, not necessarily when the user logs in successfully.

I don't know what kind of data is returned from the server, you'll have to check the data object.

success: function(data){
    if( ...check the data object to see if login successful... ) location.reload();
    else{
        // do stuff for failed login
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM