使用 jQuery 将表单发布到 Web 方法变得非常困惑

发布于 2024-12-21 22:32:53 字数 1328 浏览 0 评论 0原文

我有一个包含相当多值的表单。我目前正在使用 POST 方法提交,但我希望将其转换为通过 AJAX 使用 jQuery 来完成。

我一直在尝试将代码隐藏的“保存到数据库”功能转换为 Web 方法。我使用两步函数方法,首先准备 Request.Form("parameterName") 中的所有变量,然后将它们全部传递到“saveData”函数中,如下所示:

<System.Web.Services.WebMethod()> _
Public Shared Sub prepareRequestFormValues()
    /// save to database and do stuff with the variables
    variableName = HttpContext.Current.Request("parameterName")
    saveToDatabase(list, of, form, variables, ...)
End Sub

读完这篇文章 将表单序列化到 asp.net webmethod

我可以看到使用 jQuery 表单插件我可以序列化整个表单并将其作为单个变量发送到处理代码。

然而,我现在已经完全困惑了。鉴于我对 AJAX/codebehind 等还很陌生,我一直在兜圈子,现在很困惑!

使用

var queryString = $('input').fieldSerialize(); 

$.ajax({
   type: "POST",
   url: "Detail.aspx/doPostTest",
   data: "{ 'txtQuery': '" + queryString + "'}",
   contentType: "application/json; charset=utf-8",
   dataType: "json",
   success: function (data) {
        var products = eval(data.d);                             
        console.log(products);
   }
});

从上面提到的帖子,我可以看到我可以发送这个字符串,但是收到后如何在服务器端处理它?该文章暗示 HttpContext.Current.Request.Form("parameterName") 应该可以工作,但事实并非如此......我猜是因为我需要从 txtQuery 变量访问它。 ..但是...我恐怕迷路了!

I have a form which contains a fairly large number of values. I am currently submitting with a POST method, but I wish to convert this to do it with jQuery via AJAX.

I have been experimenting with converting my code-behind "save to database" function into a Web Method. I am using a two-step function method, firstly preparing all the variables from Request.Form("parameterName") and then passing them all into a "saveData" function as follows:

<System.Web.Services.WebMethod()> _
Public Shared Sub prepareRequestFormValues()
    /// save to database and do stuff with the variables
    variableName = HttpContext.Current.Request("parameterName")
    saveToDatabase(list, of, form, variables, ...)
End Sub

Having read this post Send form serialize to asp.net webmethod

I can see that using the jQuery forms plugin I can serialize the whole form and send it as a single variable to the processing code.

However, I have got myself completely confused now. Given that I am pretty new to AJAX/codebehind and so on I've been going round in circles and now am very confused!

Using

var queryString = $('input').fieldSerialize(); 

$.ajax({
   type: "POST",
   url: "Detail.aspx/doPostTest",
   data: "{ 'txtQuery': '" + queryString + "'}",
   contentType: "application/json; charset=utf-8",
   dataType: "json",
   success: function (data) {
        var products = eval(data.d);                             
        console.log(products);
   }
});

From the post mentioned above, I can see I can send this string, but how do I process it server-side when recieved? The article implied that HttpContext.Current.Request.Form("parameterName") should work, but it doesn't... I'm guessing because I need to access it from the txtQuery variable.... but.... I'm lost I am afraid!

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

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

发布评论

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

评论(1

于我来说 2024-12-28 22:32:53

这是一个非常好的教程 包含执行此操作的 JS。不要发布到 PHP,而是发布到您的 aspx 页面。这是有类似问题的论坛帖子。 XIII发布了以下代码。

<script src="../Scripts/jquery-1.6.1.js" type="text/javascript"></script>

<script type="text/javascript">

    $(function () {
        $('#btnAddToCart').click(function () {
            var result = $.ajax({
                type: "POST",
                url: "PassInParameterAndUpdateSession.aspx/AddProductToCart",
                data: '{ pID: "1833", qty: "13", lblType: "1" }',
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: succeeded,
                failure: function (msg) {
                    alert(msg);
                },
                error: function (xhr, err) {
                    alert(err);
                }
            });
        });
    });

    function succeeded(msg) {
        alert(msg.d);
    }

</script>

还有你的WebMethod

Imports System.Web
Imports System.Web.Services
Imports System.Web.Script.Services

    Namespace aspnetforums.AjaxAndSession
        <ScriptService> _
        Public Partial Class PassInParameterAndUpdateSession
            Inherits System.Web.UI.Page
            Protected Sub Page_Load(sender As Object, e As EventArgs)
                Session("test") = ""
            End Sub

            <WebMethod> _
            Public Shared Function AddProductToCart(pID As String, qty As String, lblType As String) As String
                Dim selectedProduct As String = String.Format("+ {0} - {1} - {2}", pID, qty, lblType)

                HttpContext.Current.Session("test") += selectedProduct

                Return HttpContext.Current.Session("test").ToString()
            End Function
        End Class
End Namespace

Here is a really good tutorial containing the JS to do this. Instead of posting to PHP post to your aspx page. Here is an forum post with a similar question. XIII had posted the follwoing code.

<script src="../Scripts/jquery-1.6.1.js" type="text/javascript"></script>

<script type="text/javascript">

    $(function () {
        $('#btnAddToCart').click(function () {
            var result = $.ajax({
                type: "POST",
                url: "PassInParameterAndUpdateSession.aspx/AddProductToCart",
                data: '{ pID: "1833", qty: "13", lblType: "1" }',
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: succeeded,
                failure: function (msg) {
                    alert(msg);
                },
                error: function (xhr, err) {
                    alert(err);
                }
            });
        });
    });

    function succeeded(msg) {
        alert(msg.d);
    }

</script>

And your WebMethod

Imports System.Web
Imports System.Web.Services
Imports System.Web.Script.Services

    Namespace aspnetforums.AjaxAndSession
        <ScriptService> _
        Public Partial Class PassInParameterAndUpdateSession
            Inherits System.Web.UI.Page
            Protected Sub Page_Load(sender As Object, e As EventArgs)
                Session("test") = ""
            End Sub

            <WebMethod> _
            Public Shared Function AddProductToCart(pID As String, qty As String, lblType As String) As String
                Dim selectedProduct As String = String.Format("+ {0} - {1} - {2}", pID, qty, lblType)

                HttpContext.Current.Session("test") += selectedProduct

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