简体   繁体   English

在jQuery Ajax函数中返回响应

[英]Returning Response in jquery ajax function

Getting problems in Response.d , based on the result which is returning by the checkusers() function I am saving the values. 根据checkusers()函数返回的结果,在Response.d中遇到问题,我正在保存这些值。 If the entered name is in already in database it should say "User already exists", if it is not in database it should create a new record. 如果输入的名称已存在于数据库中,则应显示“用户已存在”;如果输入的名称不在数据库中,则应创建一个新记录。

But I am not getting the correct value from (response), I observed that Console.log(response.d) giving me correct values like 'true' or 'false'. 但是我没有从(response)得到正确的值,我观察到Console.log(response.d)给了我正确的值,例如'true'或'false'。 I tried everything I know like- 我尝试了所有我知道的东西-

  1. changing async:"false" 更改异步:“ false”
  2. var jqXHR = $.ajax({ and returning jqXHR.responseText var jqXHR = $.ajax({并返回jqXHR.responseText

But none of they worked for me . 但是他们都没有为我工作。 Please help me with this. 请帮我解决一下这个。

submitHandler: function (form) {

        var txtName = $("#txtName").val();
        var txtEmail = $("#txtEmail").val();
        var txtSurName = $("#txtSurName").val();
        var txtMobile = $("#txtMobile").val();
        var txtAddress = $("#txtAddress").val();

        var obj = CheckUser();
        if (obj == false) {
            $.ajax({
                type: "POST",
                url: location.pathname + "/saveData",
                data: "{Name:'" + txtName + "',SurName:'" + txtSurName + "',Email:'" + txtEmail + "',Mobile:'" + txtMobile + "',Address:'" + txtAddress + "'}",
                contentType: "application/json; charset=utf-8",
                datatype: "jsondata",
                async: "true",
                success: function (response) {

                    $(".errMsg ul").remove();
                    var myObject = eval('(' + response.d + ')');
                    if (myObject > 0) {
                        bindData();
                        $(".errMsg").append("<ul><li>Data saved successfully</li></ul>");
                    }
                    else {
                        $(".errMsg").append("<ul><li>Opppps something went wrong.</li></ul>");
                    }
                    $(".errMsg").show("slow");
                    clear();

                },
                error: function (response) {
                    alert(response.status + ' ' + response.statusText);
                }


            });
        }
        else {

            $(".errMsg").append("<ul><li>User Already Exists </li></ul>");
            $(".errMsg").show("slow");

        }

    }

});

$("#btnSave").click(function () {
    $("#form1").submit()

});
});

checkusers function is: checkusers函数是:

function CheckUser() {

  var EmpName = $("#txtName").val();

    $.ajax({

    type: "POST",
    url: location.pathname + "/UserExist",
    data: "{Name:'" + EmpName + "'}",
    contentType: "application/json; charset=utf-8",
    datatype: "jsondata",
    async: "true",
    success: function (response) {
        console.log(response.d);

    },
    error: function (response) {
        alert(response.status + ' ' + response.statusText);


    }



});
}

Just because your database returns true or false doesn't mean this also gets returned by your CheckUser(). 仅仅因为您的数据库返回true或false并不意味着您的CheckUser()也会返回此值。

There are several options here: 这里有几个选项:

  1. Either you make a local variable in your CheckUser, Make your Ajax call synchronous, set the local variable to response.d in the success function and then return that local variable. 您可以在CheckUser中创建一个局部变量,使Ajax调用同步,在成功函数中将该局部变量设置为response.d,然后返回该局部变量。

  2. Another option is to work with Deferred objects and make your submithandler Ajax call wait for the Checkuser Ajax call to return; 另一种选择是使用Deferred对象,并使您的Submithandler Ajax调用等待Checkuser Ajax调用返回。

  3. A third option is to call your create ajax call from your success callback in your CheckUser Ajax call if the user isn't created yet. 第三种选择是,如果尚未创建用户,则从CheckUser Ajax调用中的成功回调中调用您的create ajax调用。

I would recommend either option 2 or 3, because option 1 is not userfriendly. 我建议选择选项2或3,因为选项1不是用户友好的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM