2011-04-05 70 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

相关问题