Have doubts about the execution order of ajax

if (isPass = = true) {

    //
    $.ajax({
        type:"post",
        url :"/user/register",
        data:$("form").serialize(),
        dataType:"json",
        success:function(data){
            alert(data);
            if(data == "success"){
                alert("");
                window.location.href="/";//
            }else if(data == "fail"){
                alert(":"+data);
            }

        },
        error:function(){
            //window.location.href="/111";
            alert("");
            return false;
        }
    });
    alert("1");
    return true;
}

after executing the code, why does the registration exception 1 dialog box pop up first, and then the registration exception dialog box

Mar.11,2021

because ajax is asynchronous


ajax is Asynchronous Javascript And XML, that is, the requests it makes are asynchronous.
what is async? For example, if you turn on the washing machine to wash clothes, and then go to dinner, the washing machine will ring several times to remind you that you have finished washing, and at this time you go to collect your clothes, and the whole process is asynchronous.
if you have to wait for the laundry before going to dinner, the process is Synchronize.
so the callback function in ajax () parentheses in your code will only remind the code to come back and execute the corresponding method when the success and error events occur, and execute the code that follows ajax () when the success and error events do not occur.
Don't you know I made myself clear?


$.ajax({
    ....
    error:function(){
        //window.location.href="/111";
        alert("");
        return false;
    }
});
alert("1");
//$.ajax()alert("1"),error
....
Menu