我正在尝试使用Jquery Validate并调用webservice来验证字段。但是当我这样做时,我得到了Firebug错误:
标签无效 { “d”:假}
这是我的代码,有人可以帮忙吗?
$("form").validate({
//errorLabelContainer: $("#divErrors"),
rules: {
txtUserName: {
required: true,
minlength: 4,
maxlength: 20,
remote: function() {
var r = {
type: "POST",
url: "/Services/CDServices.asmx/CheckForUniqueUserName",
contentType: "application/json; charset=utf-8",
dataType: "json",
data: "{'strUserName':'" + $('input[name="txtUserName"]').val() + "'}"
}
return r;
}
}
[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public bool CheckForUniqueUserName(string strUserName)
{
return false;
}
答案 0 :(得分:3)
在ASP.NET中,服务器返回.d作为返回数据的前缀。看看这个简单的例子:
$.ajax({
type: "POST",
url: "RSSReader.asmx/GetRSSReader",
data: "{}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function(msg) {
// Hide the fake progress indicator graphic.
$('#RSSContent').removeClass('loading');
// Insert the returned HTML into the <div>.
$('#RSSContent').html(msg.d);
}
});
可在http://encosia.com/2008/03/27/using-jquery-to-consume-aspnet-json-web-services/
处阅读其他信息答案 1 :(得分:1)
在您的情况下,您可以使用dataFilter函数来“解包”您的结果,如下所示:
$("form").validate(
//errorLabelContainer: $("#divErrors"),
rules: {
txtUserName: {
required: true,
minlength: 4,
maxlength: 20,
remote: function() {
var r = {
type: "POST",
url: "/Services/CDServices.asmx/CheckForUniqueUserName",
contentType: "application/json; charset=utf-8",
dataType: "json",
data: "{'strUserName':'" + $('input[name="txtUserName"]').val() + "'}",
dataFilter: function (data, dataType) {
if (dataType == "json") {
var result = $.parseJSON(data);
if (result.d) {
return result.d;
} else {
return data;
}
} else {
return data;
}
}
}
return r;
}
}
}
});