未定义的响应,ajax 不起作用?

undefined response, ajax is not working?

我在前端开发方面很新手,我正在尝试使用 Modal 插入数据,我有两个文本框,第一个是代码,第二个是名称.. 我需要它们是独一无二的:

 $.validator.addMethod("codeNotUsed", function (value, element) {
                    var response;
                    $.ajax({
                        type: "POST",
                        url: "Profile_ValidatieProfileCode.ashx",
                        data: { code: $('#txtCode').val() },
                        async: false,
                        success: function (data) {
                            response = eval(data);
                        }
                    });
                    alert(response);
                    return response;

                }, "Code already used");

 $.validator.addMethod("nameNotUsed", function (value, element) {
                    var response;
                    $.ajax({
                        type: "POST",
                        url: "Profile_ValidateProfileName.ashx",
                        data: { name: $('#txtName').val() },
                        async: false,
                        success: function (data) {
                            response = eval(data);
                        }
                    });

                    return response;

                }, "Name already used");

其中一个 .ashx 文件的内容:

 Imports System.Web
 Imports System.Web.Services

 Public Class Profile_ValidateProfileName
Implements System.Web.IHttpHandler

Sub ProcessRequest(ByVal context As HttpContext) Implements IHttpHandler.ProcessRequest

    context.Response.ContentType = "text/plain"

    Dim name = context.Request.Form("name")

    Try
        Dim type = DataFactory.Instance.GetProfileByName(UserIdentity.ClientConfig.ConnectionString, name)

        If IsNothing(type) Then
            context.Response.Write("true")
        Else
            context.Response.Write("false")
        End If

    Catch ex As Exception
        context.Response.Write("false")
    End Try


End Sub

ReadOnly Property IsReusable() As Boolean Implements IHttpHandler.IsReusable
    Get
        Return False
    End Get
End Property

结束Class

HTML代码:

 <form id="InputForm" name="InputForm" action="javascript:void(0);" novalidate="novalidate"><div id="InputFormContent">
        <br>
        <div class="row">
            <div class="col-xs-12">
                <div class="form-group">
                    <label>Code</label>
                    <div class="icon-addon">
                        <input id="txtCode" name="txtCode" class="form-control required" maxlength="10">
                        <i class="fa fa-edit"></i>
                    </div>
                </div>
            </div>
        </div>

         <div class="row">
            <div class="col-xs-12">
                <div class="form-group">
                    <label>Name</label>
                    <div class="icon-addon">
                        <input id="txtName" name="txtName" class="form-control required" maxlength="30">
                        <i class="fa fa-edit"></i>
                    </div>
                </div>
            </div>
        </div>

    </div>

唯一验证无效,我认为 ajax 代码无效,警报给了我 'Undefined'..

你的调用ajax无效,因为你必须先调用ajax,完成后检查结果并调用&.validator.addMethod

$.ajax({
    type: "POST",
    url: "Profile_ValidatieProfileCode.ashx",
    data: { code: $('#txtCode').val() }
}).done(function (data) {
    var response = eval(data);
    alert(response);
    $.validator.addMethod("codeNotUsed",function (value, element) { return response;}, "Code already used");
});

$.ajax({
    type: "POST",
    url: "Profile_ValidateProfileName.ashx",
    data: { name: $('#txtName').val() }
}).done(function (data) {
    var response = eval(data);
    alert(response);
    $.validator.addMethod("nameNotUsed",function (value, element) { return response;}, "Name already used");
});

警报语句甚至在调用成功函数之前就已执行,在 ajax 请求完成后执行警报语句在这种情况下效果更好: 代码:

$.validator.addMethod("codeNotUsed", function (value, element) {

                    var response;
                    $.ajax({
                        type: "POST",
                        url: "Profile_ValidatieProfileCode.ashx",
                        data: { code: $('#txtCode').val() },
                        async: false,
                        error: function (xhr, status, error) {
                            alert(error)
                        }
                    }).done(function (data) {
                        response = eval(data);
                        alert(response); // this will give true or false accourding to server response
                    });
                    return response;


                }, "Code already used");

                $.validator.addMethod("nameNotUsed", function (value, element) {


                        var response;
                        $.ajax({
                            type: "POST",
                            url: "Profile_ValidateProfileName.ashx",
                            data: { name: $('#txtName').val() },
                            async: false,
                            error: function (xhr, status, error) {
                                alert(error)
                            }
                        }).done(function (data) {
                            response = eval(data);
                            alert(response); // this will give true or false accourding to server response

                        });

                        return response;
                    }, "Name already used");