简体   繁体   中英

Ajax event only working once

when i enter the wrong details and run it. it pops up with the error message, but if i then enter the correct details and click run it again. the sign in button changes to "Connecting..." as it should but then nothing else happens

$(document).ready(function() {

    var width = ( $(".main").width() - 5);
    if (width < 300) {
        $(".logo-img").css({"width":width});
    };

    $("#error").hide();

    $(function() {
        $('#login').on('click', function(e){
            e.preventDefault();

            var token = $('#token').val();
            var username = $('#username').val();
            var password = $('#password').val();
            var remember = $('#remember:checked').val();

            $.ajax({
                url: 'core/functions/ajaxLogin.php',
                method: 'POST',
                data: { 'username' : username,
                        'password' : password, 
                        'remember' : remember, 
                        'token' : token },
                dataType: 'html',
                cache: false,
                beforeSend: function() { $('#login').val('Connecting...') },
                success: function( data ) {
                    if (data == 'success') {
                        setTimeout( "window.location.href='backorderbook';", 500 );
                    } else if( data == 'userorpass' ) {
                        $('#error').fadeIn();
                        $('#error_message')
                           .html('username or password were entered incorrectly');
                        $('#error').delay(3500).fadeOut();
                        $('#login').val('Sign In');
                    };
                }
            });
        });
    });
});

Reason behind working once.

when your ajax fired, first thing to do is show connecting.. then when you get response which is data your statement says

if data == success //redirects

elseif data == userpass //show you have invalid username/password and clear html

So what if your data is not succes / userpass

it will just run your ajax beforeSend() and not will remove connecting that seems to you running once.

I recommend that your data should be an object and check if there's an error with the message on it , in short have it on your backend and just jquery show that message

There is a token generated when the login page is loaded and sent with the Ajax. But my PHP token system doesn't like the same token being sent over and over and blocks the request.

用Fiddler运行你的函数..和/或将错误参数添加到你的ajax ...赔率是你的网络请求不成功。

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