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?