How to make JQuery-AJAX request synchronous

前端 未结 7 1985
佛祖请我去吃肉
佛祖请我去吃肉 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:41

    I added dataType as json and made the response as json:

    PHP

    echo json_encode(array('success'=>$res)); //send the response as json **use this instead of echo $res in your php file**
    

    JavaScript

      var ajaxSubmit = function(formE1) {
    
            var password = $.trim($('#employee_password').val());    
            $.ajax({
                type: "POST",
                async: "false",
                url: "checkpass.php",
                data: "password="+password,
                dataType:'json',  //added this so the response is in json
                success: function(result) {
                    var arr=result.success;
                    if(arr == "Successful")
                    {    return true;
                    }
                    else
                    {    return false;
                    }
                }
            });
    
      return false
    }
    
    0 讨论(0)
提交回复
热议问题