2011-04-05 125 views
-1

我使用jquery.validate,當您爲submitHandler指定一個函數時,是否可以使用它來調用某個方法,例如執行其他驗證,然後調用另一個方法來寫入數據庫?我在我的submitHandler中指定了一個方法,但是我得到錯誤'invalid label {「d」:0}',而不是做我以前見過的JSON post類型,這就是它在Firebug中所做的:Jquery驗證並提交處理程序

/CheckForUniqueUserName?callback = jQuery15109870035168010857_1301997329736

這是我的代碼,有人可以幫忙嗎?

$.validator.setDefaults({ 
      submitHandler: function() { addUser(); } 
     }); 

     $("form").validate({ 
     //errorLabelContainer: $("#divErrors"), 

      rules: { 
       txtUserName: { 
        required: true, 
        minlength: 4, 
        maxlength: 20 
       }, 
       txtPassword: { 
        required: true, 
        minlength: 4, 
        maxlength: 32 
       }, 
       txtConfirmPassword: { 
        required: true, 
        equalTo: "#txtPassword", 
        minlength: 4, 
        maxlength: 32 
       }, 
       txtFirstName: { 
        required: true, 
        maxlength: 50 
       }, 
       txtLastName: { 
        required: true, 
        maxlength: 50 
       }, 
       txtJobTitle: { 
        required: true, 
        maxlength: 100 
       }, 
       txtEmailAddress: { 
        required: true, 
        email: true, 
        maxlength: 100 
       }, 
       txtTelephoneNumber: { 
        required: true, 
        number: true//, 
        //postalCode:true 
       } 
      }, 
      messages: { 
       txtUserName: { 
        required: "Please enter a User Name", 
        minlength: "User Name must be at least 4 characters", 
        maxlength: "User Name must be no more than 20 characters" 
       }, 
       txtPassword: { 
        required: "Please enter a Password", 
        minlength: "Password must be at least 4 characters", 
        maxlength: "Password must be no more than 32 characters" 
       }, 
       txtConfirmPassword: { 
        required: "Please confirm Password", 
        equalTo: "Confirm Password must match Password", 
        minlength: "Confirm Password must be at least 4 characters", 
        maxlength: "Confirm Password must be no more than 32 characters" 
       }, 
       txtFirstName: { 
        required: "Please enter a First Name", 
        maxlength: "First Name must be no more than 50 characters" 
       }, 
       txtLastName: { 
        required: "Please enter a Last Name", 
        maxlength: "Last Name must be no more than 50 characters" 
       }, 
       txtJobTitle: { 
        required: "Please enter a Job Title", 
        maxlength: "Job Title must be no more than 100 characters" 
       }, 
       txtEmailAddress: { 
        required: "Please enter an Email Address", 
        email: "Please enter a valid Email Address", 
        maxlength: "Email Address must be no more than 100 characters" 
       }, 
       txtTelephoneNumber: { 
        required: "Please enter a Telephone Number", 
        number: "Telephone Number must be numeric" 
       } 
      } 
     }); 
    }); 

    function addUser() { 

     //check for unique username and email 
     $.ajax(
     { 
      type: "POST", 
      url: "/Services/CDServices.asmx/CheckForUniqueUserName", 
      data: "{strUserName:'" + $('input[name="txtUserName"]').val() + "'}", 
      async: false, 
      dataType: "json", 
      contentType: "application/json; charset=utf-8", 
      success: function(msg) { 
       if (msg.d == 0) { 
        alert("already exists"); 
       } 
       else { 
        alert("username is unique"); 
       } 
      } 
     }); 

[WebMethod] 
    [ScriptMethod(ResponseFormat = ResponseFormat.Json)] 
    public int CheckForUniqueUserName(string strUserName) 
    { 
     return 0; 
    } 

這是來自Web服務的響應...當我通過它似乎返回正確的值碼的步驟,但在Firebug這是出現在標籤效應初探:

System.InvalidOperationException: There was an error reflecting type 'CD.Database.DataProvider'. ---> System.InvalidOperationException: There was an error reflecting property 'Meters'. ---> System.InvalidOperationException: There was an error reflecting type 'CD.Database.Meter'. ---> System.InvalidOperationException: There was an error reflecting property 'tblCRCFootprintReportMeters'. ---> System.InvalidOperationException: There was an error reflecting type 'CD.Database.tblCRCFootprintReportMeter'. ---> System.InvalidOperationException: Cannot serialize member 'CD.Database.tblCRCFootprintReportMeter.dateUpdated' of type 'System.Data.Linq.Binary', see inner exception for more details. ---> System.InvalidOperationException: System.Data.Linq.Binary cannot be serialized because it does not have a parameterless constructor. 
    --- End of inner exception stack trace --- 
    at System.Xml.Serialization.StructModel.CheckSupportedMember(TypeDesc typeDesc, MemberInfo member, Type type) 
    at System.Xml.Serialization.StructModel.GetPropertyModel(PropertyInfo propertyInfo) 
    at System.Xml.Serialization.StructModel.GetFieldModel(MemberInfo memberInfo) 
    at System.Xml.Serialization.XmlReflectionImporter.InitializeStructMembers(StructMapping mapping, StructModel model, Boolean openModel, String typeName, RecursionLimiter limiter) 
    at System.Xml.Serialization.XmlReflectionImporter.ImportStructLikeMapping(StructModel model, String ns, Boolean openModel, XmlAttributes a, RecursionLimiter limiter) 
    at System.Xml.Serialization.XmlReflectionImporter.ImportTypeMapping(TypeModel model, String ns, ImportContext context, String dataType, XmlAttributes a, Boolean repeats, Boolean openModel, RecursionLimiter limiter) 
    --- End of inner exception stack trace --- 
    at System.Xml.Serialization.XmlReflectionImporter.ImportTypeMapping(TypeModel model, String ns, ImportContext context, String dataType, XmlAttributes a, Boolean repeats, Boolean openModel, RecursionLimiter limiter) 
    at System.Xml.Serialization.XmlReflectionImporter.CreateArrayElementsFromAttributes(ArrayMapping arrayMapping, XmlArrayItemAttributes attributes, Type arrayElementType, String arrayElementNs, RecursionLimiter limiter) 
    at System.Xml.Serialization.XmlReflectionImporter.ImportArrayLikeMapping(ArrayModel model, String ns, RecursionLimiter limiter) 
    at System.Xml.Serialization.XmlReflectionImporter.ImportAccessorMapping(MemberMapping accessor, FieldModel model, XmlAttributes a, String ns, Type choiceIdentifierType, Boolean rpc, Boolean openModel, RecursionLimiter limiter) 
    at System.Xml.Serialization.XmlReflectionImporter.ImportFieldMapping(StructModel parent, FieldModel model, XmlAttributes a, String ns, RecursionLimiter limiter) 
    at System.Xml.Serialization.XmlReflectionImporter.InitializeStructMembers(StructMapping mapping, StructModel model, Boolean openModel, String typeName, RecursionLimiter limiter) 
    --- End of inner exception stack trace --- 
    at System.Xml.Serialization.XmlReflectionImporter.InitializeStructMembers(StructMapping mapping, StructModel model, Boolean openModel, String typeName, RecursionLimiter limiter) 
    at System.Xml.Serialization.XmlReflectionImporter.ImportStructLikeMapping(StructModel model, String ns, Boolean openModel, XmlAttributes a, RecursionLimiter limiter) 
    at System.Xml.Serialization.XmlReflectionImporter.ImportTypeMapping(TypeModel model, String ns, ImportContext context, String dataType, XmlAttributes a, Boolean repeats, Boolean openModel, RecursionLimiter limiter) 
    --- End of inner exception stack trace --- 
    at System.Xml.Serialization.XmlReflectionImporter.ImportTypeMapping(TypeModel model, String ns, ImportContext context, String dataType, XmlAttributes a, Boolean repeats, Boolean openModel, RecursionLimiter limiter) 
    at System.Xml.Serialization.XmlReflectionImporter.CreateArrayElementsFromAttributes(ArrayMapping arrayMapping, XmlArrayItemAttributes attributes, Type arrayElementType, String arrayElementNs, RecursionLimiter limiter) 
    at System.Xml.Serialization.XmlReflectionImporter.ImportArrayLikeMapping(ArrayModel model, String ns, RecursionLimiter limiter) 
    at System.Xml.Serialization.XmlReflectionImporter.ImportAccessorMapping(MemberMapping accessor, FieldModel model, XmlAttributes a, String ns, Type choiceIdentifierType, Boolean rpc, Boolean openModel, RecursionLimiter limiter) 
    at System.Xml.Serialization.XmlReflectionImporter.ImportFieldMapping(StructModel parent, FieldModel model, XmlAttributes a, String ns, RecursionLimiter limiter) 
    at System.Xml.Serialization.XmlReflectionImporter.InitializeStructMembers(StructMapping mapping, StructModel model, Boolean openModel, String typeName, RecursionLimiter limiter) 
    --- End of inner exception stack trace --- 
    at System.Xml.Serialization.XmlReflectionImporter.InitializeStructMembers(StructMapping mapping, StructModel model, Boolean openModel, String typeName, RecursionLimiter limiter) 
    at System.Xml.Serialization.XmlReflectionImporter.ImportStructLikeMapping(StructModel model, String ns, Boolean openModel, XmlAttributes a, RecursionLimiter limiter) 
    at System.Xml.Serialization.XmlReflectionImporter.ImportTypeMapping(TypeModel model, String ns, ImportContext context, String dataType, XmlAttributes a, Boolean repeats, Boolean openModel, RecursionLimiter limiter) 
    --- End of inner exception stack trace --- 
    at System.Xml.Serialization.XmlReflectionImporter.ImportTypeMapping(TypeModel model, String ns, ImportContext context, String dataType, XmlAttributes a, Boolean repeats, Boolean openModel, RecursionLimiter limiter) 
    at System.Xml.Serialization.XmlReflectionImporter.ImportTypeMapping(TypeModel model, String ns, ImportContext context, String dataType, XmlAttributes a, RecursionLimiter limiter) 
    at System.Xml.Serialization.XmlReflectionImporter.ImportElement(TypeModel model, XmlRootAttribute root, String defaultNamespace, RecursionLimiter limiter) 
    at System.Xml.Serialization.XmlReflectionImporter.ImportTypeMapping(Type type, XmlRootAttribute root, String defaultNamespace) 
    at System.Xml.Serialization.XmlReflectionImporter.ImportTypeMapping(Type type, XmlRootAttribute root) 
    at System.Web.Services.Protocols.XmlReturn.GetInitializers(LogicalMethodInfo[] methodInfos) 
    at System.Web.Services.Protocols.XmlReturnWriter.GetInitializers(LogicalMethodInfo[] methodInfos) 
    at System.Web.Services.Protocols.MimeFormatter.GetInitializers(Type type, LogicalMethodInfo[] methodInfos) 
    at System.Web.Services.Protocols.HttpServerType..ctor(Type type) 
    at System.Web.Services.Protocols.HttpServerProtocol.Initialize() 
    at System.Web.Services.Protocols.ServerProtocol.SetContext(Type type, HttpContext context, HttpRequest request, HttpResponse response) 
    at System.Web.Services.Protocols.ServerProtocolFactory.Create(Type type, HttpContext context, HttpRequest request, HttpResponse response, Boolean& abortProcessing) 

回答

0

刪除行

contentType: "application/json; charset=utf-8", 

和用於數據的代碼變更爲

data: ({ strUserName: $('input[name="txtUserName"]').val() }), 

你的要求應該是這樣的

$.ajax(
    { 
    type: "POST", 
    url: "/Services/CDServices.asmx/CheckForUniqueUserName", 
    data: ({ strUserName: $('input[name="txtUserName"]').val() }), 
    async: false, 
    dataType: "json" 
    success: function(msg) { 
       if (msg.d == 0) { 
        alert("already exists"); 
       } 
       else { 
        alert("username is unique"); 
       } 
      } 
}); 

然後在螢火蟲檢查... 希望這有助於....

+0

謝謝,但是這給了我沿着線長的錯誤信息'有反映類型的錯誤...'和'System.Data.Linq.Binary無法序列化,因爲它沒有無參數的構造函數'。任何想法如何解決這一問題?在我從JSON調用Web服務的項目中,我從來沒有遇到任何問題。 – user517406 2011-04-05 10:35:43

+0

你什麼時候收到錯誤?在重新請求期間或響應期間......並且上面的代碼使您的螢火蟲正確顯示請求? – 2011-04-05 10:55:34

+0

該請求在Firebug中顯示的方式與我原來的帖子中相同。 Web服務返回正確的數據,所以錯誤必須在響應中。 – user517406 2011-04-05 11:03:57