我的应用程序中有一个Ajax请求,它返回一个json对象:
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);
}
但是返回19而不是19 transactions
,然后返回此消息(我可以从“网络”标签中看到):
警告(512):无法发出标题。标头已发送 file = / var / www / vhosts / domain / public_html / demo / app / src / Controller / InvoicesController.php line = 508 [ CORE / src / Http / ResponseEmitter.php ,行 48 ]代码上下文
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:
< / p>007700">} else {
none;“> $ response = object(Cake \ Http \ Response){
'状态'=>(int)200,'contentType'=> 'text / html','headers'=> [ 'Content-Type'=> [[[到达的最大深度]]], 'file'=> null,'fileRange'=> [], 'cookies'=>对象(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 ='无法发出标头。标头 发送 文件= / var / www / vhosts / domain / public_html / demo / app / src / Controller / InvoicesController.php line = 508'Cake \ Http \ ResponseEmitter :: emit()- CORE / src / Http / ResponseEmitter.php,第48行Cake \ Http \ Server :: emit()- CORE / src / Http / Server.php,第141行
所以我遇到了这个JavaScript错误:
未捕获到的SyntaxError:JSON中的意外令牌<位于位置6922
因为响应中包含html代码。
我尝试在json_encode
中设置depth选项,但得到的结果相同。
更新
我最终使用:
if($this->request->is('ajax')){
Configure::write('debug', 0);
}
在控制器中。但这是正确的方法吗?