【问题标题】:Redirect Url after success response from server服务器成功响应后重定向 URL
【发布时间】:2019-01-11 09:23:54
【问题描述】:

我正在使用 php 发出 GET 请求。响应后,它必须将我重定向到响应 URL,但我不知道该怎么做。响应为json格式,如下:

{"orderId":"308fea18-9288-460e-8c24-e2******","formUrl":"https://test.bank.com/payment/merchants/payment.html?mdOrder=308fea18-e2c68d9d3a2e&language=en"}

此响应是在付款请求成功后将我重定向到包含卡信息的页面。

这是表单请求:

<form name="PaymentForm" action="https://test.bank.com/payment/register.do" method="GET" id="formPayment">
<input type="hidden" name="userName" id="userName" value="api">
<input type="hidden" name="password" id="password" value="api2">
<input type="hidden" name="orderNumber" id="orderNumber" value="00000116">
<input type="hidden" name="amount" id="amount" value="1000">
<input type="hidden" name="description" id="description" value="description">
<input type="hidden" name="language" id="language" value="en">
<input type="hidden" name="pageView" id="pageView" value="DESKTOP">
<input type="hidden" name="clientId" id="clientId" value="2">

//a button for payment confirmation
<input value="Изпрати" type="submit" id="buttonPayment" class="btn btn-lg btn-primary">
</form>

请求成功后如何自动重定向用户?

我搜索了许多链接,但不知何故无法解决我的问题。

现在我尝试使用 ajax 发送数据,但我发现了其他错误:

Access to XMLHttpRequest at 'https://test.bank.com/payment/register.do?[object%20FormData]' from origin 'http://site.domain.com' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.

这是一个ajax:

 $( "#buttonPayment" ).click(function() {
    var modal = $(this);
    $.ajax({
        url: "https://ecomtest.dskbank.bg/payment/rest/register.do",
        crossDomain: true,
        method: "GET",
        data: new FormData($('#formPayment')[0]), // The form with the file inputs.
        processData: false, // Using FormData, no need to process data.
        contentType: false, // Using FormData, no need to process data.
        success: function (data) {
            console.log(data)
        },
        error: function () {
            console.log("error");
        }
    });
});

【问题讨论】:

  • 关键字是Ajax。您在用户浏览器中通过 JavaScript 发出请求。然后你检查它的返回值并做其他事情。 (例如,如果支付成功,将用户重定向到新页面)
  • 我必须使用 Ajax 发送参数并成功尝试捕获响应?

标签: php json redirect


【解决方案1】:

您必须先解码您的 json 对象,然后获取 url。

<?php
$json = '{"orderId":"308fea18-9288-460e-8c24-e2******","formUrl":"https://test.bank.com/payment/merchants/payment.html?mdOrder=308fea18-e2c68d9d3a2e&language=en"}';
$json_decoded = json_decode($json); //Decode the json object
$url = $json_decoded->formUrl; //Get the url
header("Location: $url"); //Pass the url into redirection

【讨论】:

  • 是的,但是响应在用户浏览器上,我无法使用 post 方法捕捉到答案响应......也许 paskl 对 ajax 是正确的......
  • 用户浏览器是什么意思?您能否为您的问题添加更多信息
  • 在其他域上(在银行域上,而不是将我重定向到我的应用程序)
  • 您想将您的代码添加到另一个网站?目的是什么
  • 我不知道怎么做,我必须得到回应,为此我问...机构只说必须是脚本...
猜你喜欢
  • 2017-10-18
  • 2018-05-28
  • 2020-02-27
  • 2020-07-18
  • 2018-11-23
  • 1970-01-01
  • 2016-04-11
  • 1970-01-01
  • 2018-12-08
相关资源
最近更新 更多