douzhangbao2187 2013-07-09 19:25
浏览 40
已采纳

使用knockout.js和CakePHP失败的GET请求

I'm incorporating paypal into my site , however, once I receive the token and try to redirect to paypal's site, nothing happens. I'm using knockout.js on my front end. When I click a button to make a GET request, the values are sent over to a corresponding CakePHP action which verifies the data and then redirect to paypal's site to finish the transaction.

Now here's the strange part. At first I assumed I messed up the jQuery to make the GET but that doesn't seem to be the case. When I make the GET request with the header('location...') code included in the corresponding action, the network tab shows the request failed as "canceled". However, when I comment out the header('location..') code the request goes through successfully.

Here's GET request on

//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) {

});

Here's the action that the request goes to

//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 ) ;  
    }
}

I know my calls to paypal are working correctly to get the token, because when I copy/paste the link into the address bar it then redirects (meaning the token was successfully received). This to me seems like it's an issue with jQuery making the request. However, I'm confused as to why the GET request fails when I have the header redirect included, but when it's commented out the GET passes.

Update

I swapped out the header redirect for the following and it doesn't fail, however, it doesn't redirect either. This code showing up in the response as well, so I think this probably means CakePHP does not like header('location') redirects.

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

1条回答 默认 最新

  • dt4233 2013-07-09 20:42
    关注

    getJson expects json

    $.getJSON is a method for requesting json, it's not appropriate for making a request which is expected to return not-json.

    Further, if the response only emits redirect headers the (sub) request will get redirected, not the page making the (xhr) request.

    redirect with javascript

    Given the example code, one way to achieve the desired result is to modfy the request to return valid json:

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

    And then use an appropriate success handler:

    $.getJSON(url, function(data) {
        if (data.success) {
            document.location = data.url;
        }
    });
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何用stata画出文献中常见的安慰剂检验图
  • ¥15 c语言链表结构体数据插入
  • ¥40 使用MATLAB解答线性代数问题
  • ¥15 COCOS的问题COCOS的问题
  • ¥15 FPGA-SRIO初始化失败
  • ¥15 MapReduce实现倒排索引失败
  • ¥15 ZABBIX6.0L连接数据库报错,如何解决?(操作系统-centos)
  • ¥15 找一位技术过硬的游戏pj程序员
  • ¥15 matlab生成电测深三层曲线模型代码
  • ¥50 随机森林与房贷信用风险模型