简体   繁体   English

如何检查ajax请求是否已经通过Jquery发送?

[英]How to check whether an ajax request has allready been sent with Jquery?

I am using an Ajax request to post a form with Jquery. 我正在使用Ajax请求通过Jquery发布表单。

$.ajax( 
        { 
            type: "POST", 
            url: "login.php", 
            data: $("#signin").serialize(),
            dataType: "json",
            cache: false,
            success: function(data, textStatus) {
                if (data.redirect) {
                    window.location.replace(data.redirect);
                }
                else {
                    $('#some').fadeOut(200);
                    $('#some2').fadeIn(200);
                    $("#some3").html(data.form);
                    $("#some").delay(2000).fadeOut(200);
                    $('#some2').delay(2800).fadeIn(300);
                }
            }           
        });

Now the ajax request will take place as soon as you click on a button "Login". 现在,只要单击按钮“登录”,就会发出ajax请求。 The problem now is that if you press the button more than once the else case will be executed several times which will cause #some, #some2 and #some3 to fade out and in several times. 现在的问题是,如果您多次按下按钮,则else case将被执行几次,这将导致#some,#some2和#some3淡入淡出并出现几次。 So how could I check whether the request has allready been sent (without having to write something into my db)? 那么,如何检查请求是否已全部发送(而不必在数据库中写入内容)呢?

Make boolean flag, say, login_in_process , on login check this flag in true value. 在登录时设置布尔值标志,例如login_in_process ,请检查此标志为true值。 And check this flag on every click if it true then make empty return. 并在每次点击时检查此标志(如果为true然后返回空值。 In success and error callbacks set it in false state. 成功和错误回调会将其设置为false状态。

You can use a boolean value to record whether or not it has been clicked: 您可以使用布尔值来记录是否已单击它:

var loginClicked = false;

$('input_button_element').click(function(){
  if (!loginClicked) {
    loginClicked = true;
    // your js here - you may want to add some visual feedback to the user also
  }
});

you can make a global var 你可以做一个全局变量

var loginClick = false;

Inside your method you first check that value 在方法内部,首先检查该值

if (!loginClick) {
  loginClick = true;
  //your ajax code;
}

From here : 这里

You can use .one() method and set it again in ajax callback. 您可以使用.one()方法,然后在ajax回调中再次进行设置。

function doAjax(){
    // Your Ajax call.
    $.ajax({..., complete: function() {
        // Ajax call done, re-enabling the button/link
        $("#buttonId").one('click', doAjax);
    }, ...});
}

$("#buttonId").one('click', doAjax);

You will have to store a boolean in a global scope, eg one stored on the window object: 您将必须在全局范围内存储一个布尔值,例如,一个布尔值存储在window对象上:

if (!window.isClicked) {
    window.isClicked = true;
    ...Do your ajax call here        
}

Remember to ALWAYS restore the value of window.isClicked, not only in the success callback of ajax(): 请记住,不仅要在ajax()的成功回调中,始终要还原window.isClicked的值:

var jqxhr = $.ajax( ... )
.done(function() {  })
.fail(function() {  })
.always(function() { window.isClicked = false });

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

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