如何在 ASP.NET Web 表单中实际使用 JQuery Validation 远程验证

发布于 2024-09-10 06:47:15 字数 105 浏览 1 评论 0原文

我到处搜索,但似乎找不到可靠的例子。

有人这样做吗?任何人都可以提供通过 aspx 页面(Web 表单)中的 WebMethod 使用 JQuery 验证插件调用远程验证的示例吗?

I have searched everywhere, and I can't seem to find a solid example.

Is anyone doing this? Can anyone provide and example of calling a remote validation using the JQuery validation plugin through a WebMethod in an aspx page (Web Forms)?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

韬韬不绝 2024-09-17 06:47:15

我认为代码给你带来不好的品味的原因是因为将 json 写成字符串非常难闻。一个小小的改进是创建一个真正的 JSON 对象,然后使用 JSON.stringify(...) 函数。

使用您的 json 对象创建一个变量,这可以让您在设计时和运行时进行语法检查,

var customerInput = {"customerToAssignTo":$("#customerToAssignTo").val()};
var serializedCustomerInput = JSON.stringify(customerInput );

然后您可以将该行替换

data: "{'customerToAssignTo':'" + $("#customerToAssignTo").val() + "'}"

data: serializedCustomerInput

您需要包含 https://github.com/douglascrockford/JSON-js/blob/master/json2.js

更多信息:

http://www.json.org/js.html

http://msdn.microsoft.com/en-us/library/cc836459(VS.85).aspx - 这是在 Windows 脚本编写的上下文中,但给出了功能的良好描述

I think the reason that code gives you a bad taste is because writing json as strings is pretty smelly. One slight improvement would be to create a real JSON object and then use the JSON.stringify(...) function.

Create a variable with your json object, this gives you syntax checking at design time and run time

var customerInput = {"customerToAssignTo":$("#customerToAssignTo").val()};
var serializedCustomerInput = JSON.stringify(customerInput );

then you could replace the line

data: "{'customerToAssignTo':'" + $("#customerToAssignTo").val() + "'}"

with

data: serializedCustomerInput

you would need to include https://github.com/douglascrockford/JSON-js/blob/master/json2.js

more info:

http://www.json.org/js.html

http://msdn.microsoft.com/en-us/library/cc836459(VS.85).aspx - this is in the context of windows scripting but gives a good description of the function

维持三分热 2024-09-17 06:47:15

正如我发布此文章时,我发现了一种有效的方法,但我真的不喜欢它。
非常麻烦,需要自己构建Json,这似乎不是一个好的解决方案。

var validated = $("#aspnetForm").validate(
    {
        rules:
        {
            customersToReassign:
            {
                required: true
            },
            customerToAssignTo:
            {
                required: true,
                remote: 
                {
                    url: window.location + "/IsValidCustomer",
                    type: "POST",
                    contentType: "application/json; charset=utf-8",
                    dataType: "json",
                    data: "{'customerToAssignTo':'" + $("#customerToAssignTo").val() + "'}"
                }
            }
        },

Just as I posted this I found a way that works, but I really don't like it.
It is very cumbersome and requires constructing your own Json, which doesn't seem like a good solution.

var validated = $("#aspnetForm").validate(
    {
        rules:
        {
            customersToReassign:
            {
                required: true
            },
            customerToAssignTo:
            {
                required: true,
                remote: 
                {
                    url: window.location + "/IsValidCustomer",
                    type: "POST",
                    contentType: "application/json; charset=utf-8",
                    dataType: "json",
                    data: "{'customerToAssignTo':'" + $("#customerToAssignTo").val() + "'}"
                }
            }
        },
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文