Cakephp 3 - Ajax Response/Request


#1

Hello everyone, im trying to do an autocomplete finder by ajax but i cant reach a correct repsonse, can anyone help me, thanks

AJAX:

$(document).ready(function() { $('#search').keyup(function() { var input = $("#search").val(); if( input === " " || input === "" || input === null){ $('#results').slideUp('slow'); $("#results tbody").empty(); } else if( input !== " " || input !== "" || input !== null){ $('#results').slideDown('slow'); $("#results tbody").empty(); $.ajax({ type: 'POST', url: "dashboard/searchEmployees", dataType: "json", data: { 'cadena' : input }, success : function(data){ console.log(data.responseText); $("#results tbody").empty(); CONTROLLER: public function searchEmployees(){ $this->autoRender = false; $cadena = $this->request->data['cadena']; $test = "Hola mundo"; $emps = $this->Employees->find('all', array( 'conditions' => array('Employees.Active' => 1), 'contain' => array( 'Persons' => array( 'conditions' =>array('Persons.FirstName LIKE' => '%'.$cadena.'%'))) ))->contain(['Persons']); $array = array(); foreach ($emps->all() as $value) { array_push($array, (array)$value); } $this->response->type('json'); $this->response->statusCode(200); $this->response->body(json_encode($emps)); $this->response->send(); //echo json_encode($emps); }

#2

Try end your controller function with this: (from my working program)

$data['clients'] = $clients->toArray();
$data['status'] = 'success';
$data['code'] = '200';
echo json_encode($data);

of course change variables names for yours.