在jQuery Ajax函数中返回响应 [英] Returning Response in jquery ajax function

查看:99
本文介绍了在jQuery Ajax函数中返回响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据checkusers()函数返回的结果,在Response.d中遇到问题,我正在保存这些值.如果输入的名称已存在于数据库中,则应显示用户已经存在";如果输入的名称不在数据库中,则应创建一个新记录.

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.

但是我没有从(响应)获得正确的值,我观察到Console.log(response.d)给了我正确的值,例如'true'或'false'.我尝试了所有我知道的东西-

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. 更改异步:"false"
  2. var jqXHR = $.ajax({并返回jqXHR.responseText
  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函数是:

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


    }



});
}

推荐答案

仅因为您的数据库返回true或false并不意味着CheckUser()也将返回此值.

Just because your database returns true or false doesn't mean this also gets returned by your CheckUser().

这里有几个选项:

  1. 要么在CheckUser中创建局部变量,要么使Ajax调用同步,然后在成功函数中将局部变量设置为response.d,然后返回该局部变量.

  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.

另一种选择是使用Deferred对象,并使您的Submithandler Ajax调用等待Checkuser Ajax调用返回;

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

第三个选择是,如果尚未创建用户,则从CheckUser Ajax调用的成功回调中调用您的create ajax调用.

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.

我建议选择选项2或3,因为选项1不友好.

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

这篇关于在jQuery Ajax函数中返回响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆