2017-11-12 4 views
0

私は非常にグリーンです。フロントエンドの開発については、私はModalを使ってデータを挿入しようとしています.2つのテキストボックスがあり、最初はコード、2番目は名前です。 私はそれらを一意にする必要があります。未定義の応答、ajaxは機能していませんか?

$.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 

エンドクラス

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コードがうまく機能していない独自の検証が働いて、私されていないもの、アラートが「未定義」私を与えます..あなたは、AJAXを呼び出す必要がありますので、

+0

可能性のある重複した[なぜ私は、関数の内部でそれを変更した後、私の変数変更されていないのですか? - 非同期コードリファレンス](https://stackoverflow.com/questions/23667086/why-is-my-variable-unaltered-after-i-modify-it-inside-of-a-function-asynchron) – Xufox

答えて

0

呼び出します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"); 
0

あなたの呼び出し元のアヤックスは、無効です第一及び完了後、チェック成功機能をした後でも、警告文を実行し、呼び出される前に警告文が実行された結果と& .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"); 
}); 
関連する問題