jquery远程验证与asp.net webmethod问题

发布于 2024-10-03 23:53:41 字数 1226 浏览 0 评论 0原文

我正在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

生生漫 2024-10-10 23:53:41

好吧,它似乎不会影响你的结果,但你确实有 2 个 data: 字段。

除此之外,您将 Remote: 规则视为常规的 jquery ajax 调用......它不是那样的。它没有成功回调等...它应该看起来更像这样。

remote: {
        type: "post",
        url: "WebForm1.aspx/IsValid",
        data: { username: function () {
            return $("#UserName").val();
                                      }
              }
        }

并且您的服务器端会根据发布的值是真还是假来回显“真”或“假”,这不是通过成功回调完成的。

所以

 public static Boolean IsValid()
{

  document.write("true");
}

基本上,它会命中 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.

remote: {
        type: "post",
        url: "WebForm1.aspx/IsValid",
        data: { username: function () {
            return $("#UserName").val();
                                      }
              }
        }

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

 public static Boolean IsValid()
{

  document.write("true");
}

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...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文