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

问题描述 投票:0回答:1

我面临着一个非常烦人的问题,但我无法解决......

我目前正在一个相当大的项目中使用 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>

我真的很困惑,因为这段代码是在临时/产品服务器上使用的,并且工作正常,就好像我的 Apache 安装或 PHP 安装是错误的(插件丢失,跨域请求,...)但是我搜索了所有已知问题,但似乎没有任何效果。

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

预先感谢,我真的为此苦苦挣扎了一段时间。

php ajax apache cakephp
1个回答
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']);


    }
© www.soinside.com 2019 - 2024. All rights reserved.