【问题标题】:Javascript post request callback, redirect from .NET MVC controllerJavascript 发布请求回调,从 .NET MVC 控制器重定向
【发布时间】:2019-08-06 01:05:57
【问题描述】:

我正在将 PayPal 结帐与电子商务解决方案集成,在 PayPal 成功创建 PayPal 订单/付款后,我执行一些服务器端处理,最终返回 RedirectResult(相应的付款失败或成功的 URL ) 从我的控制器返回到客户端/前端。

我在下面有以下代码,并希望它自动重定向,但不会发生重定向。

paypal.Buttons({
    createOrder: function (data, actions) {
        return actions.order.create({
            intent: "CAPTURE",
            purchase_units: [{
                amount: {
                    value: '5.20',
                }
            }]
        });
    },
    onApprove: function (data, actions) {
        return actions.order.capture().then(function (details) {
            return fetch('/umbraco/surface/PayPalPayment/process', {
                method: 'post',
                redirect: 'follow',
                body: JSON.stringify({
                    OrderID: data.orderID,
                    PayerID: data.payerID,
                }),
                headers: {
                    'content-type': 'application/json'
                }
            });
        }).catch(error=>console.log("Error capturing order!", error));
    }
}).render('#paypal-button-container');

如果我使用下面的代码显式重定向,则执行该操作。

onApprove: function (data, actions) {
        return actions.order.capture().then(function (details) {
            return fetch('/umbraco/surface/PayPalPayment/process', {
                method: 'post',
                redirect: 'follow',
                body: JSON.stringify({
                    OrderID: data.orderID,
                    PayerID: data.payerID,
                }),
                headers: {
                    'content-type': 'application/json'
                }
            }).then(function () { window.location.replace('https://www.google.co.uk') });
        }).catch(function (error) {
            console.log("Error capturing order!", error);
            window.location.replace('https://www.bbc.co.uk');
        });
    }

基本上,我想知道为什么 fetch 重定向不遵循从我的控制器返回的重定向。控制器重定向以获得完整的完整性:

return new RedirectResult("/checkout/thank-you") ;

【问题讨论】:

  • Basically, I'm wondering why fetch redirect does not follow the Redirect that is returned form my controller- 你能进一步解释一下吗?

标签: asp.net-mvc paypal paypal-sandbox fetch-api


【解决方案1】:

让我试着改一下你的问题

您想知道为什么在您发出 fetch浏览器 没有重定向 - 即使 fetch api 响应 是RedirectResult

原因很简单,你在fetch发了一个请求,也就是说你在做ajax请求(所以浏览器不会变)

您将redirect 设置为follow,这意味着在第一个请求之后(即在从 /umbraco/surface/PayPalPayment/process),它将跟随到 url /checkout/thank-you 所以,你在then() 中得到的将是/checkout/thank-you 的响应

总的来说,它确实遵循了响应,但可能不是您预期的方式(遵循 ajax 请求,而不是浏览器更改页面)

如果你想要的是重定向到特定页面,在成功调用/umbraco/surface/PayPalPayment/process之后

然后做:

  1. 修改您的后端以返回网址的JsonResult 而不是RedirectResult
return Json(new {redirectUrl = "/checkout/thank-you"});
  1. 使用then 重定向
// other code omitted

.then(function (response) { return response.json(); })
.then(function (data) {window.location.replace(data.redirectUrl)});

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-06-08
    • 1970-01-01
    • 1970-01-01
    • 2023-02-13
    • 2023-03-28
    • 1970-01-01
    • 2021-08-05
    相关资源
    最近更新 更多