繁体   English   中英

如何将javascript方法(JSON对象)的结果发送到asp.net Webform上的隐藏代码?

[英]How do I send the results of a javascript method (JSON object) to the code-behind on asp.net webform?

我有一段javascript处理我的一些表单数据,并且需要将结果提交到我的后台代码,因此我可以将其映射到对象并保存到数据库:

形式如下:

<form method="POST" id="paymentForm">
    <span class="payment-errors" runat="server"></span>

    <div>
        <label>
            <span>Card Number</span>
            <br />
            <input type="text" size="20" data-stripe="number" runat="server" placeholder="1111222233334444" />
        </label>
    </div>

    <div>
        <label>
            <span>CVC</span>
            <br />
            <input type="text" size="4" data-stripe="cvc" runat="server" placeholder="123"/>
        </label>
    </div>

    <div>
        <label>
            <span>Expiration (MM/YYYY)</span>
            <br />
            <input type="text" size="2" data-stripe="exp-month" runat="server" placeholder="01"/>
        </label>
        <br />
        <input type="text" size="4" data-stripe="exp-year" runat="server" placeholder="2020"/>
    </div>
    <button type="submit">Submit Payment</button>
</form>

这是js位:

<script type="text/javascript">
    // This identifies your website in the createToken call below
    // You need to put your real publish key here.
    Stripe.setPublishableKey('pk_test_1nDJ3hA1Mv2Sy9bUoYcBMXmm');
    // ...
    // I am using jquery to process the payment. It knows what form to 
    // process it on based on the name 'payment-form'
    jQuery(function ($) {
        //payment submission
        $('#ctl01').submit(function (event) {
            alert('In .submit()');
            var $form = $(this);

            // Disable the submit button to prevent repeated clicks
            $form.find('button').prop('disabled', true);

            Stripe.createToken($form, stripeResponseHandler);

            // Prevent the form from submitting with the default action
            return false;
        });

        //if there is a error, it is displayed on the page if there was
        //no error this is where it gets sent to the server.
        var stripeResponseHandler = function (status, response) {
            var $form = $('#ctl01');

            if (response.error) {
                // Show the errors on the form
                $form.find('.payment-errors').text(response.error.message);
                $form.find('button').prop('disabled', false);
            } else {
                // token contains id, last4, and card type
                var token = response.id;
                // Insert the token into the form so it gets submitted to the server
                $form.append($('<input type="hidden" name="stripeToken" />').val(token));
                // and submit
                $form.get(0).submit();
            }
        };
    });
</script>

因此,js创建了Stripe令牌,但我不知道如何在单击Submit时将其打到我的代码后面。 页面仅闪烁,没有任何内容。

我试图使按钮成为一个与事件相关联的asp.net按钮,这不好。

我是否应该在Page_Load()中执行一些操作,以使这些数据可以在POST到页面中?

我看到了两个即时选项,具体取决于您要在服务器端代码中执行的操作:

  • 手动调用__doPostBack JavaScript函数以导致服务器端回发。 如果您想与页面上的ASP.NET服务器控件进行交互,我将推荐这种方法。

这是另一个StackOverflow问题,详细说明如何在__doPostBack中发送多个参数

  • 调用ASP.NET AJAX页面方法,该方法本质上是承载在ASP.NET页面内部的独立Web服务。 如果您只想调用一些服务器端逻辑(即从数据库中保存或检索数据),而又不需要页面本身的实例,那么我会推荐这种方法,因为这是异步操作,并且页面都不存在控件将可用。 这非常适合仅将数据发送到服务器并取回jQuery可以处理的数据。

这是使用jQuery直接调用ASP.NET AJAX页面方法的方法

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM