将数据从knockout.js发布到CakePHP控制器

时间:2013-05-17 02:16:12

标签: php jquery json cakephp knockout.js

我正在将knockout.js页面中的数据发布到cakephp中的控制器,并且它表示数据已成功发布,但是,我的控制器似乎没有响应,我也没有收到回复的警告..甚至不是空响应。我甚至检查了chrome中的网络选项卡,它显示了正确的POSTED数据

这是从我的knockout viewmodel文件中发布的数据

var JSON_order = JSON.stringify({"orderInfo":[{"itemNumber":"1","quantity":"1","price":1.00,"productName":"test"}]});
$.post("/orders/submit_order", JSON_order,
function(data){
    alert(data.check); //alert doesn't appear
}, "json");

这是我的控制器

function submit_order(){
    $this->layout = false;
    $this->autoRender = false;
    if ($this->request->is('post')) {
        $order = $this->request->data;
        $order = json_decode($order, true);
        $finalize_order = new submit;
        $finalize_order->display_submitted_order_success($order);
    }
}

这是display_submitted_order_success的代码(我也在CakePHP之外的php文件上尝试了这个,但它也没有用)

function display_submitted_order_success($order = null){
    $this->layout = false;
    $this->autoRender = false;
      //I'm just trying to display the order as-is so that I know it's even being posted to begin with
    echo json_encode(array("check" => "success","order_num" => $order)); //the values passed the price check, display the result 
}

1 个答案:

答案 0 :(得分:1)

您必须将JSON_order的值分配给var:

var JSON_order = JSON.stringify({"orderInfo":[{"itemNumber":"1","quantity":"1","price":1.00,"productName":"test"}]});
$.post("/orders/submit_order", {order:JSON_order},
function(data){
    alert(data.check); //alert doesn't appear
}, "json");

这样你的控制器就会收到它:

$data['order'] = '{"orderInfo":[{"itemNumber":"1","quantity":"1","price":1,"productName":"test"}]}'