服务器成功响应后重定向URL

时间:2019-01-11 09:23:55

标签: php json redirect

我正在使用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");
        }
    });
});

1 个答案:

答案 0 :(得分: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