I have autocomplete and other ajax calls working fine in my local development set up but on a test server, they return the cakephp site home page instead of the json data. Could it be my apache setup?
Here’s an abbreviated example of one of my controller functions, in this case in PeopleController.php
public function search()
{
$this->Authorization->skipAuthorization();
$this->autoRender = false;
if ($this->request->is('ajax')) {
$people = $this->getPeopleAutocomplete($_GET['term']);
if ($people) {
sort($people);
$this->response = $this->response->withType('json')->withStringBody(json_encode($people));
return $this->response;
}
}
}
JS autocomplete function:
export const autocompleteField = (inputFieldId, valueFieldId, url, callback) => {
$('#' + inputFieldId).autocomplete({
minLength: minAutocompleteLength,
delay: 500,
source: function (request, response) {
$.getJSON(url, request, function (data, status, xhr) {
if (!data) {
response();
$("#" + inputFieldId).val(ui.item.label).removeClass("ui-autocomplete-loading");
} else {
response(data);
}
});
},
select: function (event, ui) {
$("#" + valueFieldId).val(ui.item.value);
$("#" + inputFieldId).val(ui.item.label).removeClass("ui-autocomplete-loading");
if (callback) {
callback();
}
return false;
}
});
}