【问题标题】:Retrieving JSON data in controller from URL with cakephp使用 cakephp 从 URL 中检索控制器中的 JSON 数据
【发布时间】:2013-05-10 18:59:46
【问题描述】:

我正在尝试将 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'];

【问题讨论】:

    标签: php json cakephp knockout.js


    【解决方案1】:

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

    【讨论】:

    • 我正在尝试这样做,但我遇到了一些问题。我发了一个帖子here介意看看吗?
    【解决方案2】:

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

    查看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
    

    【讨论】:

    • 我应该如何解码这个值,因为我是从我的 CakePHP 控制器而不是 JavaScript 中检索它的?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-02
    • 2014-11-27
    • 1970-01-01
    • 2017-11-20
    • 1970-01-01
    相关资源
    最近更新 更多