0

我正在将贝宝合并到我的网站中,但是,一旦我收到令牌并尝试重定向到贝宝的网站,什么也没有发生。我在前端使用了 knockout.js。当我单击一个按钮发出 GET 请求时,这些值被发送到相应的 CakePHP 操作,该操作验证数据,然后重定向到贝宝的网站以完成交易。

现在这是奇怪的部分。起初我以为我搞砸了 jQuery 来制作 GET,但事实并非如此。当我使用包含在相应操作中的标头('位置...')代码发出 GET 请求时,网络选项卡将请求失败显示为“已取消”。但是,当我注释掉 header('location..') 代码时,请求成功通过。

这是 GET 请求

//order_processing.js

$.getJSON("/orders/pay_for_order/" + itemNumbers + "/" + quantities + "/" + prices + "/" + productNames + "/" + self.fullName() + "/" + self.addressLine1() + " " + self.addressLine2() + "/" + self.city() + "/" + self.state() + "/" + self.ZIP() + "/" + self.email(),function(data) {

});

这是请求执行的操作

//OrdersController

function pay_for_order($id = null){
     //processing code here
     //get token from paypal

    if($finalize_order->verifyPrice($item_numbers, $prices) == TRUE){  
        //when I comment out this header redirect the GET request is successful, but of course then does not redirect
        header( 'Location: https://www.sandbox.paypal.com/cgi-bin/webscr?cmd=_express-checkout&token='.$token ) ;  
    }
}

我知道我对贝宝的调用可以正常工作以获取令牌,因为当我将链接复制/粘贴到地址栏中时,它会重定向(意味着成功接收到令牌)。这对我来说似乎是 jQuery 发出请求的问题。但是,我很困惑为什么当我包含标头重定向时 GET 请求失败,但是当它被注释掉 GET 传递时。

更新

我将标头重定向换成了以下内容,它没有失败,但是,它也没有重定向。该代码也出现在响应中,所以我认为这可能意味着 CakePHP 不喜欢 header('location') 重定向。

echo '<META HTTP-EQUIV="Refresh" Content="0; URL=https://www.sandbox.paypal.com/cgi-bin/webscr?cmd=_express-checkout&token='.$token.'">';    
4

1 回答 1

1

getJson 期望 json

$.getJSON是一种请求 json 的方法,它不适合发出预期返回 not-json 的请求。

此外,如果响应仅发出重定向标头,则(子)请求将被重定向,而不是发出(xhr)请求的页面。

使用 javascript 重定向

给定示例代码,实现所需结果的一种方法是修改请求以返回有效的 json:

{
    "success": true,
    "url": "https://www.sandbox..."
}

然后使用适当的成功处理程序:

$.getJSON(url, function(data) {
    if (data.success) {
        document.location = data.url;
    }
});
于 2013-07-09T20:42:33.827 回答