[英]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.