我正在使用jquery.validate,当你为submitHandler指定一个函数时,你能用它来调用一个方法来进行额外的验证,然后调用另一个方法写入数据库吗?我在我的submitHandler中指定了一个方法,但是我收到错误'invalid label {“d”:0}'而不是做我以前见过的JSON帖子的类型,这就是它在Firebug中的作用:
/ CheckForUniqueUserName?回调= 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;
}
这是来自webservice的响应...当我单步执行代码时,它似乎返回正确的值,但在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 :(得分: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");
}
}
});
然后检查萤火虫...... 希望这会有所帮助......