微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

异常发生时如何中止提交?

如何解决异常发生时如何中止提交?

我正在使用Stripe付款,我的代码始终可用,但是如果发生异常,则无法处理HttpPost提交中的异常。

这是用户单击按钮后的入口点:

[HttpPost]
public ActionResult Checkout_Stripe()
{
    return PayWithStripe();
}

public ActionResult PayWithStripe()
{
    ... some codes here ...
    try
    {
        ... some codes here and exception happens ...
        return Json(new { id = stripeSession.Id });
    }
    catch (Exception ex)
    {
        ... execution reached here but even with below redirect,I still get HTTP 404.
        return RedirectToAction("Index","Cart");
    }
}

和我的JavaScript

<script type="text/javascript">
    var stripe = Stripe('my stripe key');
    var checkoutButton = document.getElementById('stripe-checkout-button');

    checkoutButton.addEventListener('click',function () {
        fetch('https://localhost:44323/Cart/Checkout_Stripe',{
                method: 'POST'
            })
            .then(function(response) {
                return response.json();
            })
            .then(function (session) {
                return stripe.redirecttocheckout({ sessionId: session.id });
            })
            .then(function(result) {
                if (result.error) {
                    alert(result.error.message);
                }
            })
            .catch(function(error) {
                console.error('Error:',error);
            });
    });
</script>

这是我在浏览器中看到的:

找不到资源。说明:HTTP404。资源 正在寻找(或其依赖项之一)可能已被删除, 更名,或暂时不可用。请查阅 以下网址,并确保其拼写正确。

请求的网址:/购物车

解决方法

我需要处理JavaScript以下部分中的错误

.catch(function(error) {

    // in case of error,I need to redirect to error page here
    window.location.href = "/Cart/GatewayRedirectFailed";

    console.error('Error:',error);
});

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。