我试图将JSON编码的数据从Knockout.js传递到我的控制器中的一个动作,但它不工作并显示为null。
我在一个非蛋糕php文件中使用了相同的脚本,它工作得很好。是否有一种特殊的方式来解码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发送数据?url中的数组符号可能没有被正确转义。
您是否尝试将数据作为常规查询字符串发送,然后像在控制器中访问任何正常字符串一样访问它?
查看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