- запрос возвращает все объекты автомобилей (игнорирует get.term)
- ответ не не привязан к вводу поиска
index.ctp
$("#autocomplete").autocomplete(
{
search: function () {},
source: function (request, response)
{
$.ajax(
{
source: "/cars/autocomplete",
dataType: "json",
data:
{
term: request.term,
},
success: function (data)
{
response(data);
console.log(data);
}
});
},
minLength: 2
});
CarsController.php
function autocomplete() {
if ($this->request->is('ajax','get')) {
$term = $this->request->data["term"];
$terms = $cars->find('all', [
'conditions' => ['Cars.name >' => $term],
'limit' => 10
]);
$data = array();
foreach($terms as $term) {
$row = $term->name;
array_push($data, $row);
}
// $this->layout = 'ajax';
$this->set('terms', $terms);
echo json_encode($data);
// return json_encode($data);
}
else {
echo json_encode('Nothings found');
}
}
Подробнее здесь: https://stackoverflow.com/questions/382 ... tocomplete
Мобильная версия