【问题标题】:Why does my C# code runs regardless if JavaScript condition is met? [duplicate]为什么无论是否满足 JavaScript 条件,我的 C# 代码都会运行? [复制]
【发布时间】:2020-11-16 07:56:22
【问题描述】:

我的结帐页面上有一个 PayPal 智能按钮。我想在付款被批准后记录交易。 “onApprove”中的警报按预期工作。只有在付款被批准时才会弹出。但是,C# 卡在运行时好像不属于 JS 代码一样。 Here is the PayPal code tutorial.

<script>
    paypal.Buttons({
        createOrder: function (data, actions) {
            // This function sets up the details of the transaction, including the amount and line item details.
            return actions.order.create({
                purchase_units: [{
                    amount: {
                        value: '@Model.Price'
                    }
                }]
            });
        },
        onApprove: function (data, actions) {
            // This function captures the funds from the transaction.
            return actions.order.capture().then(function (details) {
                // This function shows a transaction success message to your buyer.
                alert('Transaction completed by ' + details.payer.name.given_name);
                @{
                    var userId = UserManager.GetUserId(User);

                    var transaction = new Transaction
                    {
                        Price = Model.Price,
                        BuyerId = userId,
                        BuyerName = User.Identity.Name,
                        SellerId = Model.AuthorId,
                        SellerName = Model.AuthorName,
                        TimeOccured = DateTime.Now,
                        ForumId = Model.ForumId,
                        ForumName = Model.ForumName
                    };

                    await _postService.AddTransaction(transaction);
                }
            });
        },
         onCancel: function (data) {
            // Show a cancel page, or return to cart
            alert('You have canceled the order');
          }

    }).render('#paypal-button-container');
      //This function displays Smart Payment Buttons on your web page.
</script>

【问题讨论】:

    标签: javascript c# asp.net paypal


    【解决方案1】:

    C# 代码确实不属于 JavaScript 代码。它在 HTML 页面(包括 JavaScript)返回到 Web 浏览器之前在服务器端运行。

    您的onApprove JavaScript 事件应该执行一个返回到服务器的调用(例如 Ajax 调用)。您的 C# 代码块应该在该回调中运行。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-08-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-07-08
      • 2016-02-15
      • 1970-01-01
      相关资源
      最近更新 更多