【问题标题】:POST data not appearing in CakePHP controllerPOST 数据未出现在 CakePHP 控制器中
【发布时间】:2013-07-18 19:42:17
【问题描述】:

我在knockout.js 表单上使用AJAX 来发布CakePHP 应该接收的一些信息,但是Cake 似乎没有找到任何东西。此外,尽管 POST 的状态为 200 (OK),但警报并未出现。

这里是 AJAX

$.ajax({  
          url: "/orders/finalize_payment",  
          type: "POST",  
          dataType: "json",  
          contentType: "json",  
          data: JSON.stringify({"customer": customer_id}),  
          success: function(){              
            alert("success");  
          }
    }); 

这是订单控制器中的相应操作。现在,我将其完全剥离到最低限度。

function finalize_payment($id = null){
    $this->layout = false;
    $this->autoRender = false;
    if($this->request->is('post')){ //the user has submitted which status to view
        print_r($this->request->data);
            echo "test"; //just to make sure it's reaching this point
    }
}

当我在 chrome 中打开网络选项卡时,它将请求负载显示为

customer: 1

POST 显示成功,状态 200。我检查了响应标头,它只是显示

array
(
)
test

尽管 chrome 显示了正在发送的有效负载,但 CakePHP 显然没有找到它。

更新

我将请求从 AJAX 更改为 $.post 并且它有效。我仍然不知道为什么

$.post("/orders/finalize_payment",{"customer_id":customer_id},function(data){
        alert('success');
 });

【问题讨论】:

    标签: php jquery ajax cakephp


    【解决方案1】:

    当您使用 CakePHP 时,您可能会发现将 RequestHandler 添加到您的组件中可以解决问题。

    public $components = array([snip], 'RequestHandler');
    

    这样做让我可以使用 $this->request->data 透明地访问 JSON 发布的数据。 other 答案的建议不要将 POST 数据编码为 JSON 变得有点尴尬,因为某些 JS 框架,例如 Angular,默认发布 JSON。

    【讨论】:

      【解决方案2】:

      不要将发布数据编码为 json

      问题中的代码不会出现在任何php脚本中,原因是这样的:

      内容类型:“json”

      这不是一个表单 URL 编码的请求,所以例如以下代码:

      print_r($_POST);
      print_r(file_get_contents('php://input'));
      

      将输出:

      Array()
      '{"customer":123}'
      

      如果您想以 json 格式提交数据,则需要阅读原始请求正文:

      $data = json_decode(file_get_contents('php://input'));
      

      有时可能需要这样做(使用 api),但这不是使用 $.post 的正常方式。

      正常方式

      提交数据的正常方式,是让jQuery为你处理编码:

      $.ajax({  
          url: "/orders/finalize_payment",  
          type: "POST",  
          dataType: "json",  // this is optional - indicates the expected response format
          data: {"customer": customer_id},  
          success: function(){              
             alert("success");  
          }
      });
      

      这会将帖子数据以application/x-www-form-urlencoded 的形式提交,并在控制器中以$this->request->data 的形式提供。

      为什么 $.post 有效

      我将请求从 AJAX 更改为 $.post 并且它有效。我仍然不知道为什么

      隐含地在您的问题中使用更新的代码:

      • 删除了 JSON.stringify 调用
      • 从提交json改为提交application/x-www-form-urlencoded

      因此,并不是$.post 有效而$.ajax 无效($.post 实际上只是调用$.ajax)- 而是结果$.ajax 调用的参数与问题。

      【讨论】:

        【解决方案3】:

        使用原始数据和json你可以使用:

        $data = $this->request->input('json_decode');
        

        **Data 现在是一个对象,而不是数组。

        那么你可以使用:

          $this->MyModel->save($data).
        

        【讨论】:

          【解决方案4】:

          格式很好的问题:)

          我很确定我有答案,虽然我可能是错的......基本上,$this->request 是 Cake 中的一个对象,$this->request->data 是一个变量/数组,它是对象的一个​​属性。

          您发送给 Cake 的数据直接进入对象(如果可能的话),而不是进入 data 数组。这就是为什么当 Cake 使用 HtmlHelper 生成表单时,名称是,例如 data[User][username]

          我认为,如果您将 JSON.stringify({"customer": customer_id}) 放入 'data' 数组并发送它,它应该可以工作。

          【讨论】:

          • 我不确定我是否理解您所说的“进入数据数组”是什么意思。我将其更改为以下内容,但没有任何更改 JSON.stringify({"data": [ {"customer": customer_id} ] })
          • 我认为你需要摆脱 JSON.stringify 并传递对象,即:data: {"customer": customer_id},或者如果这不起作用,那么:data: {"data": ["customer": customer_id]}
          • 这些都不起作用。附带说明一下,我将 $this->request->data 更改为 $this->request,data 是该数组的一部分,它是空的。此外,我在那里没有看到任何与我发送的值有关的内容。
          • 好吧,我最终将其更改为 jquery 的 $.post 并且它起作用了。我不明白为什么 AJAX 给我带来这么大的麻烦
          【解决方案5】:

          看看this post。您的数据字符串可能不正确。因此,CakePHP 可能无法将其放入$this->request->data

          【讨论】:

          • 使用$this->request->data表示版本2.x,处理顶级数据键的缺失。了解它仍然很有用,因为它有时会出乎意料 - 例如。如果你提交{foo: "bar", data: {Stuff: "zum"}}.
          【解决方案6】:

          使用 print_r($this->request->params);

          function finalize_payment($id = null){
              $this->layout = false;
              $this->autoRender = false;
              if($this->request->is('post')){ view
                  print_r($this->request->params);
              } }
          

          【讨论】:

            猜你喜欢
            • 2013-08-19
            • 1970-01-01
            • 1970-01-01
            • 2017-12-24
            • 2012-06-22
            • 2012-01-07
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多