2

I'm trying to pass JSON encoded data from Knockout.js to an action in my controller but it's not working and showing as null.

I used the same script in a non-cake php file and it worked perfectly. Is there a special way to decode json data with cake? Say this is the URL being passed.

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

Here is the action

//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"

}

Is there a special way to handle this with Cakephp? With a standard php file I'd set something like

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

And then access it with

$order = $_GET['order'];
4

2 に答える 2

1

おそらく、GET の代わりに POST 経由でデータを送信してみてください。URL の配列表記が正しくエスケープされていない可能性があります。

于 2013-05-16T02:28:43.443 に答える
0

データを通常のクエリ文字列として送信してから、コントローラーの通常の文字列と同じようにアクセスしようとしましたか?

json オブジェクトをクエリ文字列に変換できるjQuery.param()を確認してください。ページの例を次に示します。

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
于 2013-05-16T02:47:40.013 に答える