使用 WebForms 进行 jQuery 验证网络服务

发布于 2024-10-08 19:28:32 字数 1405 浏览 0 评论 0原文

现在我有一个简单的表单,只有一个输入。我正在尝试使用 jQuery Validate 的远程部分来调用我的 web 服务,该服务现在只是返回 false。我现在遇到的问题是,当它调用我的网络服务时,它会提取输入的名称,这是 .net 创建的一些垃圾。有没有办法覆盖它并使用输入的 ID 和输入的名称。这是我的 jQuery:

$(function () {
   $('form').validate();
    $("#tbSiteName").rules("add", {
        required: true,
        remote: "webservices/webservice.asmx/HelloWorld"
    });
});

这是我的 HTML:

<label for="tbSiteName">Name:</label>
<input name="ctl00$MainContent$tbSiteName" type="text" id="tbSiteName" class="required" />

这是来自 Chrome 的标头信息:(注意查询字符串参数)

Accept:application/json, text/javascript, */*
Accept-Charset:ISO-8859-1,utf-8;q=0.7,*;q=0.3
Accept-Encoding:gzip,deflate,sdch
Accept-Language:en-US,en;q=0.8
Connection:keep-alive
Content-Type:application/x-www-form-urlencoded
Cookie:ASP.NET_SessionId=qvupxcrg0yukekkni323dapj
Host:localhost:56803
Referer:http://localhost:56803/Default.aspx
User-Agent:Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US) AppleWebKit/534.10 (KHTML, like Gecko) Chrome/8.0.552.224 Safari/534.10
X-Requested-With:XMLHttpRequest
Query String Parameters
ctl00%24MainContent%24tbSiteName:ts

从服务器端,我收到 500 内部服务器错误,因为我的签名与我的帖子不匹配。

网络服务代码:

[WebMethod]
[ScriptMethod]
public bool HelloWorld(string tbSiteName) {
          return tbSiteName.Length > 5;
}

感谢您的帮助。

Right now I have simple form with a single input. I'm trying to use the remote part of jQuery Validate to call my webservice that right now is just returning false. The problem I'm having right now is when it calls my webservice it is pulling the name of the input which is some garbage created by .net. Is there a way to override this and use the Id of the input verse the name of the input. Here is my jQuery:

$(function () {
   $('form').validate();
    $("#tbSiteName").rules("add", {
        required: true,
        remote: "webservices/webservice.asmx/HelloWorld"
    });
});

Here is my HTML:

<label for="tbSiteName">Name:</label>
<input name="ctl00$MainContent$tbSiteName" type="text" id="tbSiteName" class="required" />

Here is the header info from Chrome: (notice the Query string params)

Accept:application/json, text/javascript, */*
Accept-Charset:ISO-8859-1,utf-8;q=0.7,*;q=0.3
Accept-Encoding:gzip,deflate,sdch
Accept-Language:en-US,en;q=0.8
Connection:keep-alive
Content-Type:application/x-www-form-urlencoded
Cookie:ASP.NET_SessionId=qvupxcrg0yukekkni323dapj
Host:localhost:56803
Referer:http://localhost:56803/Default.aspx
User-Agent:Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US) AppleWebKit/534.10 (KHTML, like Gecko) Chrome/8.0.552.224 Safari/534.10
X-Requested-With:XMLHttpRequest
Query String Parameters
ctl00%24MainContent%24tbSiteName:ts

From the server side I'm getting a 500 Internal server error because my signature doesn't match my post.

Webservice Code:

[WebMethod]
[ScriptMethod]
public bool HelloWorld(string tbSiteName) {
          return tbSiteName.Length > 5;
}

Thanks for the help.

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

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

发布评论

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

评论(2

只是我以为 2024-10-15 19:28:33

不幸的是,据我所知,在使用 ASP.Net WebForms 服务器控件(例如 )时,无法解决此问题。尽管在 .NET 4 中您具有 ClientIdMode="Static" 属性 (参见此处)禁用自动生成的客户端 ID,不会影响 name 属性。

Rick Strahl 在回应他的博客文章上的评论时建议,如果您确实需要可预测的名称,您应该只使用 html 控件:

ClientIDMode 仅影响 ID,而不影响控件上的 NAME 属性,因此对于回发表单元素,名称仍将是 UniqueID 中保存的长名称。恕我直言,这是合理的。如果您确实需要简单的名称,请使用普通的 INPUT 元素而不是 ASP.NET 控件,特别是如果您不依赖控件的 POSTBACK 分配来使用 Request.Form[] 检索值。

您是否考虑过仅使用客户端 而不是

此外,您是否考虑过放弃 ASP.NET WebForms 并使用 MVC? ;-)

Unfortunately, to my knowledge, there's no way to get around this when using ASP.Net WebForms server controls such as <asp:textbox>. Although in .NET 4 you have the ClientIdMode="Static" attribute (see here) to disable the auto-generated client IDs, that does not affect the name attribute.

Rick Strahl has suggested in response to comments on his blog post that if you really need predictable names, you should just use an html <input> control:

ClientIDMode only affects the ID not the NAME attribute on the control, so for post back form elements the name will still be a long name as held in UniqueID. This is reasonable though IMHO. If you really need simple names use plain INPUT elements rather than ASP.NET controls especially if you don't rely on POSTBACK assignment of controls anyway to retrieve the values by using Request.Form[].

Have you considered just using a client-side <input> instead of an <asp:textbox runat="server">?

Additionally, have you considered dropping ASP.NET WebForms and using MVC? ;-)

假面具 2024-10-15 19:28:33

这就是我想出的答案。我必须将我的 asp:textbox 更改为 HTML 输入才能使其正常工作。另外,我必须将 web.config 更改为所有 HttpGet 到我的 web 服务。这充其量是痛苦的。我还通过使用标准输入丢失了控件的视图状态。我很想将其切换到 MVC,但这不是一个选择。

感谢您的帮助。

$("#tbSiteName").rules("add", {
            required: true,
            remote:  function() {
                var r = {
                    url: "/webservices/ipmws.asmx/SiteValid",
                    type: "POST",
                    data: "{'tbSiteName': '" + $('#tbSiteName').val() + "'}",
                    dataType: "json",
                    contentType: "application/json; charset=utf-8",
                    dataFilter: function(data) { return (JSON.parse(data)).d; } 
                }
                return r
            },
        });

This is the answer I've came up with. I had to change my asp:textbox to a HTML input in order to get this to work. Also, I had to change the web.config to all HttpGet to my webservice. This is painful at best. I've also lost viewstate on the control by using a standard input. I'd love to switch this over to MVC, but it is just not an option.

Thanks for the help.

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