将数据从knockout.js发布到CakePHP控制器 [英] POST data from knockout.js to CakePHP controller
问题描述
我正在将knockout.js页面中的数据发布到cakephp中的控制器,并且它表示数据已成功发布,但是,我的控制器似乎没有响应,我也没有收到回复的警告..甚至不是空响应。我甚至检查了chrome中的网络选项卡,它显示了正确的POSTED数据
I'm posting data from a knockout.js page to a controller in cakephp and it says the data was successfully posted, however, my controller doesn't seem to be responding and I don't get an alert back...not even a null response. I even checked the network tab in chrome and it shows the correct data being POSTED
这是从我的knockout viewmodel文件中发布的数据
Here's the data being posted from my knockout viewmodel file
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文件,但它也不起作用)
Here's the code for display_submitted_order_success (I also tried this on a php file outside of CakePHP but it didn't work either)
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
}
推荐答案
您必须将 JSON_order
的值分配给var:
You have to assign the value of JSON_order
to a 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");
这样你的控制器就会收到它:
So that your controller would receive it like this:
$data['order'] = '{"orderInfo":[{"itemNumber":"1","quantity":"1","price":1,"productName":"test"}]}'
这篇关于将数据从knockout.js发布到CakePHP控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!