How to make JQuery-AJAX request synchronous

前端 未结 7 1983
佛祖请我去吃肉
佛祖请我去吃肉 2020-11-30 09:55

How do i make an ajax request synchronous?

I have a form which needs to be submitted. But it needs to be submitted only when the user enters the correct password.

7条回答
  •  失恋的感觉
    2020-11-30 10:16

    It's as simple as the one below, and works like a charm.

    My solution perfectly answers your question: How to make JQuery-AJAX request synchronous

    Set ajax to synchronous before the ajax call, and then reset it after your ajax call:

    $.ajaxSetup({async: false});
    
    $ajax({ajax call....});
    
    $.ajaxSetup({async: true});
    

    In your case it would look like this:

    $.ajaxSetup({async: false});
    
    $.ajax({
            type: "POST",
            async: "false",
            url: "checkpass.php",
            data: "password="+password,
            success: function(html) {
                var arr=$.parseJSON(html);
                if(arr == "Successful") {
                    return true;
                } else {
                    return false;
                }
            }
        });
    
    
    $.ajaxSetup({async: true});
    

    I hope it helps :)

提交回复
热议问题