javascript没有按正确的顺序工作(如果有)

javascript没有按正确的顺序工作(如果有),javascript,jquery,function,api,asp.net-web-api,Javascript,Jquery,Function,Api,Asp.net Web Api,我有一个表单,可以在保存时验证重复的名称,并且可以使用webapi进行处理。下面是一个示例函数,它通过单击按钮调用onclick=“return validateName()” function validateName() { var nam = $('#frmeditCategory input[id="Name"]').val(); var result = false; $.ajax({ type: "POST", url: "/A

我有一个表单,可以在保存时验证重复的名称,并且可以使用webapi进行处理。下面是一个示例函数,它通过单击按钮调用
onclick=“return validateName()”

function validateName() {
    var nam = $('#frmeditCategory input[id="Name"]').val();
    var result = false;
    $.ajax({
        type: "POST",
        url: "/Admin/CategoryName",
        data: { Name: "test"},
        dataType: "json",
        success: function (response) {
            if (response.message == true) {
                alert("Category already exists.");
                result = false;
                alert(0);
                return false;
            }
            else {
                result = true;
                alert("1");
                return true;
            }
        },
        error: function (xhr, ajaxOptions, thrownError) { alert("some error occured"); result = false; }
    });
    alert("2");
    return false;
}

此处
警报(“2”)首先执行,然后ajax开始工作。我很困惑,不知道自己做错了什么。请帮帮我,伙计们

该ajax调用是一个异步请求。发生的情况是,当您调用该方法时,您正在启动请求。但是,请求可能需要一段时间才能完成,直到您从服务器获取数据,或者发生错误


ajax内部的函数在数据从服务器到达时执行,这就是为什么此警报仅在警报(“2”)之后执行

该ajax调用是一个异步请求。发生的情况是,当您调用该方法时,您正在启动请求。但是,请求可能需要一段时间才能完成,直到您从服务器获取数据,或者发生错误


ajax内部的函数在数据从服务器到达时执行,这就是为什么此警报仅在警报(“2”)之后执行

Ajax调用在默认情况下是异步的,这意味着代码执行不会停止等待Ajax调用返回,而是在http请求返回和调用Ajax回调之前继续并到达您的
alert(2)
语句


您也可以执行同步ajax调用,但这通常不是一个好主意,因为在调用返回之前,其他东西都会冻结。

ajax调用默认为异步的,这意味着代码执行不会停止等待ajax调用返回,而是继续并达到您的
警报(2)
语句在http请求返回并调用ajax回调之前很久

您也可以执行同步ajax调用,但这通常不是一个好主意,因为在调用返回之前,其他东西将冻结。

alert(“2”)
需要放在
$的内部。ajax
s promise回调:

$.ajax({
    type: "POST",
    url: "/Admin/CategoryName",
    data: { Name: "test"},
    dataType: "json",
    success: function (response) {
        if (response.message == true) {
            alert("Category already exists.");
            result = false;
            // Why are the following here, they will never get called?
            // alert(0);
            // return false;
        } else {
            // Why is this line here, you never use it?
            // result = true;
            alert("1");
            return true;
        }
    },
    error: function (xhr, ajaxOptions, thrownError) {
        alert("some error occured");
        result = false;
    }
}).done(function () {
    alert("2");
});
警报(“2”)
需要放在
$内。ajax
的承诺回调:

$.ajax({
    type: "POST",
    url: "/Admin/CategoryName",
    data: { Name: "test"},
    dataType: "json",
    success: function (response) {
        if (response.message == true) {
            alert("Category already exists.");
            result = false;
            // Why are the following here, they will never get called?
            // alert(0);
            // return false;
        } else {
            // Why is this line here, you never use it?
            // result = true;
            alert("1");
            return true;
        }
    },
    error: function (xhr, ajaxOptions, thrownError) {
        alert("some error occured");
        result = false;
    }
}).done(function () {
    alert("2");
});

将async设置为false,这将在ajax调用完成后执行其余代码

  $.ajax({
        type: "POST",
        url: "/Admin/CategoryName",
        data: { Name: "test"},
        dataType: "json",
        async: false,
        success: function (response) {

            if (response.message == true) {
                alert("Category already exists.");
                result = false;
                alert(0);
                return false;
            }
            else {
                result = true;
                alert("1");
                return true;
            }
        },
        error: function (xhr, ajaxOptions, thrownError) { alert("some error occured"); result = false; }
    });

将async设置为false,这将在ajax调用完成后执行其余代码

  $.ajax({
        type: "POST",
        url: "/Admin/CategoryName",
        data: { Name: "test"},
        dataType: "json",
        async: false,
        success: function (response) {

            if (response.message == true) {
                alert("Category already exists.");
                result = false;
                alert(0);
                return false;
            }
            else {
                result = true;
                alert("1");
                return true;
            }
        },
        error: function (xhr, ajaxOptions, thrownError) { alert("some error occured"); result = false; }
    });
Ajax中的A代表“异步”,意味着Ajax成功处理程序回调将在到达第二条alert语句后很久调用。Ajax中的A代表“异步”,意味着Ajax成功处理程序回调将在到达第二条alert语句后很久调用。