这是我的自动完成视图代码,但它显示我500内部服务器错误
如果我没有使用正确的网址或其他问题,我不确定会出现什么问题。
public function search(){
$this->loadComponent('RequestHandler');
if ($this->request->is('ajax'))
{
$name = $this->request->query['term'];
$resultArr = $this->Invoices
->find()
->where(
['Invoices.name LIKE' => ($name . '%')],
['Invoices.name' => 'string']);
$resultsArr = [];
foreach ($resultArr as $result)
{
$resultsArr[] = ($result['name']);
}
$this->set('resultsArr', $resultsArr);
// This line is what handles converting your array into json
// To get this to work you must load the request handler
$this->set('_serialize', ['resultsArr']);
}
}
这是我的观看代码:
<?php echo $this->Form->input('name', ['type' => 'text']); ?>
$.ajax({
type: "POST",
url: "<?php echo Router::url(array('controller' => 'Clinics','action' => 'search')); ?>",
success: function(response) {
$("#name").autocomplete({ source: response });
}
});
答案 0 :(得分:0)
根据注释中的错误消息,未定义loadComponent方法。
我建议在控制器顶部附近加载RequestHandler组件,例如:
class ClinicController extends AppController {
public $components = array('Request');
还有另一种动态加载组件的语法:
$this->Request = $this->Components->load('Request');
参考:https://book.cakephp.org/2.0/en/controllers/components.html