【问题标题】:CakePHP 3 - Can't return proper json when debug mode = trueCakePHP 3 - 调试模式 = true 时无法返回正确的 json
【发布时间】:2016-08-26 22:58:25
【问题描述】:

我是 stackoverflow 的新手,刚刚开始使用 CakePHP 3。

我遇到了一个奇怪的问题:

我正在向控制器发送一个 ajax 请求(表单提交),我希望得到一个正确的 json 响应。当我在 config/app.php 中将调试模式设置为 false 时,它​​工作正常,但是当它设置为 true 时,我在浏览器控制台中收到一条错误消息,并且响应文本似乎是 html。我在 url 中调用带有 .json 扩展名的操作。

我已经链接了控制台的屏幕截图,其中第一个响应是调试模式设置为 false,第二个响应设置为 true:

我在 config/routes.php 中启用了扩展:

Router::scope('/', function (RouteBuilder $routes) {
    $routes->extensions(['json', 'xml']);
(...)

这是控制器代码:

public function getUserStats() {
    $this->log($this->request->data, 'debug');

    if (($this->request->is('post'))) {
        $this->log('getCategories(): Post-request is received.', 'info');

        $usersTable = TableRegistry::get('Users');
        $q = $usersTable->find('statsByUsers', $this->request->data);
        $users = $q->all();

        // Calculating total amount per user.               
        foreach ($users as $u) {
            foreach ($u->purchases as $p) {
                $u->total += $p->total;
            }
        }

        $this->log($users, 'debug');

        $this->set('users', $users);
        $this->set('_serialize', ['users']);
    }
}

这是模型代码:

 public function findStatsByUsers(Query $query, array $options) {
    debug($options);
    $options['dates'] = $this->getConvertedDates($options);
    $query
        ->contain([
            'Purchases' => function($q) use($options) {
                return $q
                    ->select(['id', 'total' => 'amount * count', 'purchase_date', 'user_id'])
                    ->where(['purchase_date BETWEEN :fromDate AND :toDate',])
                    ->bind(':fromDate', $options['dates']['fromDate'], 'datetime') // Binds the dates to the variables in where-conditions
                    ->bind(':toDate', $options['dates']['toDate'], 'datetime');
            }
                ])
            ->where([
                'Users.id IN ' => $options['users'],
                'Users.active' => true
        ]);

    return $query;
}

我希望我已经为您提供了足够的信息,以便您可以帮助我解决这个问题。

CakePHP 版本:3.3.2

【问题讨论】:

    标签: json ajax cakephp cakephp-3.3


    【解决方案1】:

    查看屏幕截图中可见的输出部分

    <div class="cake-debug-output"> ...
    

    HTML 是由debug() 函数生成的输出。

    仔细查看您的模型代码,您应该会发现对函数的调用。删除它,你应该会很好。

    顺便说一句,调用的来源可以在&lt;div&gt; 的第一个&lt;span&gt; 元素中找到,因此如果您以后遇到类似问题,请务必检查一下。

    【讨论】:

      【解决方案2】:
      <?php
      
      use Cake\Core\Configure;
      
      // your class ,...
      
      public function getUserStats() {
      
              $this->log($users, 'debug');
      
              Configure::write('debug',false); // DISABLE
      
              $this->set('users', $users);
              $this->set('_serialize', ['users']);
      }
      

      【讨论】:

      • 请编辑更多信息。不鼓励使用纯代码和“试试这个”的答案,因为它们不包含可搜索的内容,也没有解释为什么有人应该“试试这个”。我们在这里努力成为知识的资源。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-12-05
      • 2015-10-01
      • 1970-01-01
      • 1970-01-01
      • 2020-06-02
      • 1970-01-01
      • 2022-10-09
      相关资源
      最近更新 更多