jquery远程验证与asp.net webmethod问题
我正在使用 jQuery 远程验证与 asp.net 代码隐藏和 web 服务,现在远程功能可以正常命中服务器,我返回 True 或 false JSON 响应,但发生的情况如下:
第一次我故意点击登录按钮空字段我在客户端和服务器端都收到错误消息,很好,但是如果我填写该字段并尝试点击登录按钮,则远程功能工作正常,但客户端不行,即使该字段也不会出现错误消息已满。
我在网上尝试了很多搜索,但没有成功,如果有人能提供帮助,我将不胜感激。
这是我的代码:
$("#Login").click(function () {
$("#form1").validate(
// This prevents validation from running on every
// form submission by default.
// onsubmit: false
{ 规则:{ 电子邮件: { 必需:true,
remote: {
type: "POST",
url: "WebForm1.aspx/IsValid",
data: "{}",
contentType: "application/json; charset=utf-8",
dataType: "json",
data: "{'username':'" + function () {
return $("#UserName").val();
} + "'}", success: function (msg) {
result = (msg == '1') ? true : false;
// alert(msg.d);
}
}
}
}
}
);
var isValid = $("#form1").valid();
alert(isValid);
});
服务器端代码
public static Boolean IsValid()
{
return true;
}
I am using jQuery remote validation with asp.net codebehind and webservice, now the remote function is hitting the server fine and I am returning True or false JSON response but what is happening is the following:
The first time I hit the Login button with intentional empty field I get an error message both on client side and server side, fine but if I fill the field and try to hit the login button the Remote function is working fine but not client side and the error message is not going even though the field is filled.
I have tried many searches on net with no luck, I would deeply appreciate it if someone could help on this.
Here is my code:
$("#Login").click(function () {
$("#form1").validate(
// This prevents validation from running on every
// form submission by default.
// onsubmit: false
{
rules: {
Email: {
required: true,
remote: {
type: "POST",
url: "WebForm1.aspx/IsValid",
data: "{}",
contentType: "application/json; charset=utf-8",
dataType: "json",
data: "{'username':'" + function () {
return $("#UserName").val();
} + "'}", success: function (msg) {
result = (msg == '1') ? true : false;
// alert(msg.d);
}
}
}
}
}
);
var isValid = $("#form1").valid();
alert(isValid);
});
server side code
public static Boolean IsValid()
{
return true;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好吧,它似乎不会影响你的结果,但你确实有 2 个 data: 字段。
除此之外,您将 Remote: 规则视为常规的 jquery ajax 调用......它不是那样的。它没有成功回调等...它应该看起来更像这样。
并且您的服务器端会根据发布的值是真还是假来回显“真”或“假”,这不是通过成功回调完成的。
所以
基本上,它会命中 url: 你给它,将 data: 中的变量传递给它,然后等待 true 或 false 来确定其代码是否有效。当您不输入任何内容时,表单验证起作用的原因是因为需要:您检查其是否为空的 true 值...
Ok well it doesn't seem to be affecting your outcome, but you do have 2 data: fields.
Besides that, you are treating the remote: rule like a regular jquery ajax call... its not like that. It doesn't have a success callback, etc... It should look more like this.
And your server side well echo "true" or "false" based on the posted value being true or false, it's not done via a success callback.
So something like
Basically, it will hit the url: you give it, pass it the variables in data: and wait for a true or false to determine if its valid code or not. The reason your form validate works when you put nothing in, is because of the required: true value you have checks if its empty...