Ajax, prevent multiple request on click

前端 未结 10 2257
忘掉有多难
忘掉有多难 2020-11-28 04:10

I\'m trying to prevent multiple requests when user click on login or register button. This is my code, but it doesn\'t work. Just the first time works fine, then return fals

10条回答
  •  醉话见心
    2020-11-28 04:46

    In your ajax callbacks the context (this) changes from the outer function, you can set it to be the same by using the context property in $.ajax

    $.ajax({
        type: "POST",
        url: "/php/auth/login.php",
        data: $("#login-form").serialize(),
        context: this, //<-----
        success: function(msg) {
            //stuffs
        },
        complete: function() {
            $(this).data('requestRunning', false);
        }
    });      
    

提交回复
热议问题