简体   繁体   English

Ajax事件只工作一次

[英]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 当你的ajax被解雇时,首先要做的是显示连接..然后当你得到响应时,你的声明说的是data

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. 它只会运行你的ajax beforeSend()而不会删除你运行一次的连接。

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 我建议你的数据应该是一个对象并检查它上面的消息是否有错误,简而言之就是你的后端只有jquery显示该消息

There is a token generated when the login page is loaded and sent with the Ajax. 加载登录页面并使用Ajax发送时会生成令牌。 But my PHP token system doesn't like the same token being sent over and over and blocks the request. 但我的PHP令牌系统不喜欢反复发送相同的令牌并阻止请求。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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