我正在使用jQuery 1.7.2和jQuery Validation 1.8.1与asp.net。在jQuery Validation中有一个名为remote的选项,它需要对Web服务进行ajax调用。我的事情是我正在检查id是否已经使用。我使用JSON.stringify将值发布到Web服务但它将发送空字符串,但如果我发送没有JSON.stringify,我可以看到值。
我的代码如下。那么我们如何解决这个问题呢?
$(document).ready(function () {
var validator = $("#form1").validate({
rules: {
ctl00$ContentPlaceHolder$txCivilID: {
required: true,
rangelength: [12,12],
digits: true,
remote: {
url: "CivilID.aspx/IsAlreadyAvailable",
type: "post",
contentType: "application/json; charset=utf-8",
dataType: "json",
data: JSON.stringify({ civilID: $('#<%=txCivilID.ClientID%>').val() }),
dataFilter: function (data) {
var msg = JSON.parse(data);
if (msg.hasOwnProperty('d'))
return msg.d;
else
return msg;
}
}
},
ctl00$ContentPlaceHolder$txFirstName: {
required: true
},
ctl00$ContentPlaceHolder$txLastName: {
required: true
},
ctl00$ContentPlaceHolder$txMobile: {
required: true,
rangelength: [8,8],
digits: true
},
ctl00$ContentPlaceHolder$dpUserType: {
required: true
}
},
messages: {
ctl00$ContentPlaceHolder$txCivilID: {
required: "Civil for is required!",
rangelength: "Civil for should be {0} in length!",
digits: "Civil for should be only numbers!",
remote: "Civil ID already exists !!!"
},
ctl00$ContentPlaceHolder$txFirstName: "First Name is required!",
ctl00$ContentPlaceHolder$txLastName: "Last Name is required!",
ctl00$ContentPlaceHolder$txMobile: {
required: "Mobile number is required!",
rangelength: "Mobile number should be {0} in length!",
digits: "Mobile number should be only numbers!"
},
ctl00$ContentPlaceHolder$dpUserType: "Select User Type !"
},
errorPlacement: function (error, element) {
$('label[for="' + element.attr("id") + '"]').text(error.text());
}
});
});
渲染Javascript版本
<script type="text/javascript">
$(document).ready(function () {
var validator = $("#form1").validate({
rules: {
ctl00$ContentPlaceHolder$txCivilID: {
required: true,
rangelength: [12,12],
digits: true,
remote: {
url: "CivilID.aspx/IsAlreadyAvailable",
type: "post",
contentType: "application/json; charset=utf-8",
dataType: "json",
data: '{ civilID:' + JSON.stringify($('#ContentPlaceHolder_txCivilID').val()) + '}',
dataFilter: function (data) {
var msg = JSON.parse(data);
if (msg.hasOwnProperty('d'))
return msg.d;
else
return msg;
}
}
},
ctl00$ContentPlaceHolder$txFirstName: {
required: true
},
ctl00$ContentPlaceHolder$txLastName: {
required: true
},
ctl00$ContentPlaceHolder$txMobile: {
required: true,
rangelength: [8,8],
digits: true
},
ctl00$ContentPlaceHolder$dpUserType: {
required: true
}
},
messages: {
ctl00$ContentPlaceHolder$txCivilID: {
required: "Civil for is required!",
rangelength: "Civil for should be {0} in length!",
digits: "Civil for should be only numbers!",
remote: "Civil ID already exists !!!"
},
ctl00$ContentPlaceHolder$txFirstName: "First Name is required!",
ctl00$ContentPlaceHolder$txLastName: "Last Name is required!",
ctl00$ContentPlaceHolder$txMobile: {
required: "Mobile number is required!",
rangelength: "Mobile number should be {0} in length!",
digits: "Mobile number should be only numbers!"
},
ctl00$ContentPlaceHolder$dpUserType: "Select User Type !"
},
errorPlacement: function (error, element) {
$('label[for="' + element.attr("id") + '"]').text(error.text());
}
});
});
答案 0 :(得分:0)
尝试更改此行:
data: JSON.stringify({ civilID: $('#<%=txCivilID.ClientID%>').val() }),
到此......
data: JSON.stringify('{ civilID:' + $('#<%=txCivilID.ClientID%>').val() + '}'),
由于没有工作,您考虑过将数据位卸载到函数中:
function getCivilIdData() {
var data = {};
data['civilId'] = $('#<%=txCivilID.ClientID%>').val();
return data;
}
然后将您的另一行更改为:
data: JSON.stringify(getCivilIdData()),
答案 1 :(得分:0)
那里你不需要JSON.stringify()
方法。因为它已经是一个字符串。 JSON.stringify()
是将json对象转换为字符串。像这样更改代码
data: { civilID: $('#<%=txCivilID.ClientID%>').val() },
答案 2 :(得分:0)
嗯,这是我的错误,实际上远程应该像下面的函数一样使用
remote: function(){
}
上面的解决了JSON.strigify发送空字符串
的问题