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 宇视监控服务器无法登录
  • ¥15 PADS Logic 原理图
  • ¥15 PADS Logic 图标
  • ¥15 电脑和power bi环境都是英文如何将日期层次结构转换成英文
  • ¥15 DruidDataSource一直closing
  • ¥20 气象站点数据求取中~
  • ¥15 如何获取APP内弹出的网址链接
  • ¥15 wifi 图标不见了 不知道怎么办 上不了网 变成小地球了
  • ¥50 STM32单片机传感器读取错误
  • ¥50 power BI 从Mysql服务器导入数据,但连接进去后显示表无数据