Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

Getting problems in Response.d , based on the result which is returning by the checkusers() function I am saving the values. 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'. I tried everything I know like-

  1. changing async:"false"
  2. var jqXHR = $.ajax({ and returning 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:

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);


    }



});
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
174 views
Welcome To Ask or Share your Answers For Others

1 Answer

Just because your database returns true or false doesn't mean this also gets returned by your 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.

  2. Another option is to work with Deferred objects and make your submithandler Ajax call wait for the Checkuser Ajax call to return;

  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.

I would recommend either option 2 or 3, because option 1 is not userfriendly.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...