最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Return statement executes before ajax response - Stack Overflow

programmeradmin0浏览0评论

I am making an ajax call on submit button click event to check field validations server side. When I get a validation fail, ajax response gives the proper error message, returns false, and stops the form to submit to the action url. But when I get a response of 'success', the form is still not submitting to the action url script.

Is this the case when return statement executes before ajax response?

And also why is the form not getting submitted?

Here is the code:

<input type="submit" onclick="return validate();" name="submit" value="Proceed" />

<script type="text/javascript">
    var flag=false;
    function validate(){

        $.ajax({
            type:"POST",
            url:"../chk.php",
            data:datastring,
            cache: false,
            success: function (result) {


                if(result.toString() == "success" ){

                    flag=true;

                }

                else{
                    $('#error').css('display', 'block');
                    $('#error').css('color','red');
                    $('#error').text(result.toString());
                    flag=false;
                }
            }

        });


        return flag;
    }
</script>

I am making an ajax call on submit button click event to check field validations server side. When I get a validation fail, ajax response gives the proper error message, returns false, and stops the form to submit to the action url. But when I get a response of 'success', the form is still not submitting to the action url script.

Is this the case when return statement executes before ajax response?

And also why is the form not getting submitted?

Here is the code:

<input type="submit" onclick="return validate();" name="submit" value="Proceed" />

<script type="text/javascript">
    var flag=false;
    function validate(){

        $.ajax({
            type:"POST",
            url:"../chk.php",
            data:datastring,
            cache: false,
            success: function (result) {


                if(result.toString() == "success" ){

                    flag=true;

                }

                else{
                    $('#error').css('display', 'block');
                    $('#error').css('color','red');
                    $('#error').text(result.toString());
                    flag=false;
                }
            }

        });


        return flag;
    }
</script>
Share Improve this question edited Jan 1, 2015 at 6:53 Jota 17.6k7 gold badges66 silver badges93 bronze badges asked Jan 1, 2015 at 6:27 Nirali JoshiNirali Joshi 2,0086 gold badges33 silver badges50 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 8

one Way is

use async : false

Setting async to false means that the statement you are calling has to plete before the next statement in your function can be called.

$.ajax({
            type:"POST",
            url:"../chk.php",
            data:datastring,
            cache: false,
            async : false,
            success: function (result) {

And also why are you returning the value outside the ajax function , return the value inside ajax success if you are not using async : false

 $.ajax({
            type:"POST",
            url:"../chk.php",
            data:datastring,
            cache: false,
            success: function (result) {


                if(result.toString() == "success" ){

                    flag=true;

                }

                else{
                    $('#error').css('display', 'block');
                    $('#error').css('color','red');
                    $('#error').text(result.toString());
                    flag=false;
                }
            }

             return flag;

        });

Ajax is asynchronous, so you should just pass a function to the function as an argument, and then execute it on success of the ajax call.

function my_callback() {
    alert('done');
}

function validate(cb) {
    $.ajax({
        /* ... */
        success: function() {
            cb();
        }
    });
}

The function you pass to validate will be executed upon the success function call.

发布评论

评论列表(0)

  1. 暂无评论