开发者

Jquery Validate and submitHandler

I am using jquery.validate, when you specify a function for submitHandler, can you use it to call a method to for example do additional validation and then calling another method to write to the database? I have specified a method in my submitHandler, but I get the error 'invalid label {"d":0}' and rather than doing the type of JSON post I have seen before, this is what it does in Firebug :

/CheckForUniqueUserName?callback=jQuery15109870035168010857_1301997329736

Here is my code, could anybody help?

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

This is the response from the webservice...when I step through the code it seems to return the correct value, but in Firebug this is what appears in the Reponse tab :

System.InvalidOperationException: There was an error reflecting type 'CD.Database.DataProvider'. ---> System.InvalidOperationException: There was an error reflecting property 'Meters'. ---> System.InvalidOperationException: Th开发者_StackOverflow中文版ere 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)


Remove the line

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

and change the code for data to

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

Your request should look like this

$.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");
                 }
             }
 });

And then check in the firebug... Hope this helps....

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜