为了账号安全,请及时绑定邮箱和手机立即绑定

使用 AJAX 的异步请求

使用 AJAX 的异步请求

PHP
HUX布斯 2022-07-22 17:05:20
$.ajax({        async:false,        url: "ajax/stop_billed_reservation_delete.php",        type: "POST",        dataType : "json",        data : { "rid" : <?php echo $_GET['reservationId']; ?> },        success: function(result){            console.log(result);            return result;        }    });我的目标是返回 ajax 响应。但在返回响应脚本运行其他部分之前。我该如何解决这个问题!ajax 响应应该是true或者false 所以返回值应该是true或者false此脚本用于停止表单提交。如果返回值为真,表单应该提交,否则(假)不应该提交。(此代码用于验证表单)
查看完整描述

1 回答

?
qq_花开花谢_0

TA贡献1835条经验 获得超7个赞

默认情况下,Ajax 是异步的,因此它不等待 ajax 响应。async: false不会工作,因为它已经被弃用了。可以在ajax函数的success函数中运行表单提交。


$.ajax({

    url: "ajax/stop_billed_reservation_delete.php",

    type: "POST",

    dataType : "json",

    data : { "rid" : <?php echo $_GET['reservationId']; ?> },

    success: function(result){

        console.log(result);

        if(result){

            submitForm(); //Run the function that will submit the form.

        }

    }

});


function submitForm(){

    //Relevant code for submitting the form.

    . . . . . .

}


查看完整回答
反对 回复 2022-07-22
  • 1 回答
  • 0 关注
  • 72 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信