繁体   English   中英

使用cakephp从URL中检索控制器中的JSON数据

[英]Retrieving JSON data in controller from URL with cakephp

我试图将JSON编码的数据从Knockout.js传递到控制器中的某个动作,但它无法正常工作并显示为null。

我在一个非蛋糕的php文件中使用了相同的脚本,并且运行良好。 有没有一种特殊的方法可以用Cake解码JSON数据? 说这是正在传递的URL。

/orders/submit_order/%7B"orderInfo":["itemNumber":"1","quantity":"1","price":"1.00","productName":"Test Product"]%7D

这是动作

//OrdersController

function submit_order($order = null){
    $order = json_decode($order, true); 
    print_r($order);
   //I also tried commenting out the json decode to simply pass the info without further processing but that just displayed "t"

}

有没有一种特殊的方法可以用Cakephp处理呢? 使用标准的php文件,我会设置类似

 page.php?order=....json data

然后使用

$order = $_GET['order'];

也许尝试通过POST而不是GET发送数据? 网址中的数组表示法可能无法正确转义。

您是否尝试过将数据作为常规查询字符串发送,然后像访问控制器中的任何常规字符串一样进行访问?

jQuery.param() ,它应该允许您将json对象转换为查询字符串。 这是页面上的示例:

var myObject = {
  a: {
    one: 1,
    two: 2,
    three: 3
  },
  b: [1,2,3]
};
var recursiveEncoded = $.param(myObject);
var recursiveDecoded = decodeURIComponent($.param(myObject));

alert(recursiveEncoded);
alert(recursiveDecoded);

和结果(分别针对每个调用的函数):

a%5Bone%5D=1&a%5Btwo%5D=2&a%5Bthree%5D=3&b%5B%5D=1&b%5B%5D=2&b%5B%5D=3 
a[one]=1&a[two]=2&a[three]=3&b[]=1&b[]=2&b[]=3

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM