CakePHP AjaxHandler-> respond()返回错误500

时间:2017-04-20 14:37:16

标签: php ajax apache cakephp

我遇到了一个非常烦人的问题,我无法弄清楚......

我目前在一个相当大的项目中使用PHP 5.6和CakePHP 2.3,它有三个独立的存储库(每个存储库使用不同的CakePHP安装)。我在Ubuntu上的Apache本地服务器上。

网站工作正常,我可以创建数据,更新和删除它们,除非我使用Ajax和POST HTTP方法。当我这样做时,执行请求(数据被编辑/创建/删除)但函数$this->AjaxHandler->respond('json');什么都不返回,导致我的ajax发生HTTP错误500,等待响应。

我的Apache日志和CakePHP日志中没有错误消息。

我的控制器

<?php

public function edit($id = null) {

    $this->ClientPayment->id = $id;
    $this->ClientPayment->save($this->request->data)

    if ($this->request->is('ajax')){
        $this->AjaxHandler->response(true, $this->ClientPayment->read(),__('The client payment has been saved'));
        // At this point, everything went fine, clientPayment was saved
        // and $this->ClientPayment contain everything we need

        $this->AjaxHandler->respond('json');
    }

我的ajax脚本

<script>
    $(function() {
        $("#ClientPaymentEditForm").submit(function( event ) {
            ajaxSubmitForm($(this), function(data){
        });
        event.preventDefault();
    });
});
</script>

我真的很困惑,因为这个代码在staging / prod服务器上使用,它运行正常,好像我的Apache安装或我的PHP安装错误(插件缺失,跨域请求,... 。)但我搜索了所有已知的问题,似乎没有任何工作。

如果有人有一些想法来调试它,我可以收集各种信息!

在此先感谢,我已经有一段时间在努力了。

1 个答案:

答案 0 :(得分:0)

首先我首先使用Postman测试,我可以告诉你这是我这样做的方式,在我推荐使用Postman之后,在发送ajax数据之后。

   public function edit()
    {
      $data = ['result' => 'fail', 'id' => 'null'];
      $errors = $this->ClientPayment->validator()->errors($this->request->getData());
      if(empty($errors))
      {
          $clientPayment = $this->ClientPayment->newEntity($this->request->getData());

          //You can get the data sent with your variables and assign them to their correct variables

          $clientPayment->id = $this->request->getData('id');
          $clientPayment->name = $this->request->getData('name');

          if ($this->ClientPayment->save($clientPayment))
          {
              $this->set([
                    'success' => true,
                    'data' => $clientPayment,
                    '_serialize' => ['success', 'data']
                ]);
          }
      } else {

          $data['error'] = $errors;
      }
      $this->set(compact('data'));
      $this->set('_serialize', ['data']);


    }