0

I have an ajax request in my application which returns a json object:

public function getCustomerTransactions($customerid = null)
    {
        $this->viewBuilder()->setLayout(false);
        $this->autoRender = false;

        $selectedCustomerCompany = $this->request->getData('customer');
        $selectedCustomer = TableRegistry::get('Customers')->find('all', [
                            'conditions' => ['Customers.company LIKE '=> $selectedCustomerCompany]
                        ])->first();

        $customerTransactions = TableRegistry::get('Transactions')->find('all', [
                            'conditions' => ['customer_id '=> $selectedCustomer->id, 'transaction_type_id ' => 1, 'invoice_id IS NULL']//,

                        ]);

        echo json_encode($customerTransactions);
    }

But instead of 19 transactions it returns 11 and then this message (I can see it from network tab) :

Warning (512): Unable to emit headers. Headers sent in file=/var/www/vhosts/domain/public_html/demo/app/src/Controller/InvoicesController.php line=508 [CORE/src/Http/ResponseEmitter.php, line 48]Code Context 0000BB">            

style="color: #007700">if ( 0000BB">Configure::read 007700">('debug')) {

class="code-highlight"> 0000BB">                trigger_error

style="color: #007700">( 0000BB">$message, E_USER_WARNING);

            

style="color:

007700">} else {

none;">$response = object(Cake\Http\Response) {

'status' => (int) 200, 'contentType' => 'text/html', 'headers' => [ 'Content-Type' => [ [maximum depth reached] ] ], 'file' => null, 'fileRange' => [], 'cookies' => object(Cake\Http\Cookie\CookieCollection) {}, 'cacheDirectives' => [], 'body' => ''

} $maxBufferLength = (int) 8192 $file = '/var/www/vhosts/domain/public_html/demo/app/src/Controller/InvoicesController.php' $line = (int) 508 $message = 'Unable to emit headers. Headers sent in file=/var/www/vhosts/domain/public_html/demo/app/src/Controller/InvoicesController.php line=508'Cake\Http\ResponseEmitter::emit() - CORE/src/Http/ResponseEmitter.php, line 48 Cake\Http\Server::emit() - CORE/src/Http/Server.php, line 141

So I get this javascript error :

Uncaught SyntaxError: Unexpected token < in JSON at position 6922

Because there is html code in the response.

I tried to set the depth option in json_encode but I get the same results.

UPDATE

I ended up using:

if($this->request->is('ajax')){
            Configure::write('debug', 0);
        }

In the controller. But is this the correct way?

netdev
  • 496
  • 1
  • 5
  • 22
  • 1
    Alternately, you could figure out why a debug message is being created and fix that problem. – Greg Schmidt Jun 11 '19 at 15:25
  • @GregSchmidt I know thats why I didn't asnwer my question. I can't find out why this debug message is created after a number of query results. I guess that depth has nothing to do with the number of the elements of the returned array but with the depth of the returned array. For example [a, b, [1, 2, 3]] has depth 2 while [1, 2, 3, ......... 10000] has depth 1 – netdev Jun 12 '19 at 05:57
  • "_[maximum depth reached]_" is not an error, it's just a notice from the debugger that it won't inspect the (context) data further, as its maximum depth limit (defaults to `3`) has been reached (this is to avoid possible performance problems). Your problem is that you are echoing data in a controller, that's a no-go, controllers should never echo data! **https://stackoverflow.com/questions/42378793/how-to-output-custom-http-body-contents-with-cakephp-3-4-echoing-causes-unable** – ndm Jun 12 '19 at 11:43

0 Answers0