Guys, please help, I can not display the data from the request for autocomplete. I use this plugin: https://github.com/devbridge/jQuery-Autocomplete
The code is: $ (function () {
function Autocomplete(url, element){ element.autocomplete({ serviceUrl: url, onSelect: function (suggestion) { var thehtml = '<strong>Currency Name:</strong> ' + suggestion.name + ' <br> <strong>Symbol:</strong> ' + suggestion.id; $('#outputcontent').html(thehtml); } }); } $('.autocompleteSearch input.auto').keypress(function(){ Autocomplete($(this).attr('url'), $(this)); }); }); The problem is in the withdrawal of suggestion.name and suggestion.id
Returns the following JSON format from the server:
suggestion: {id:[3], name:[Клиника коррекции зрения]} I did it like this, but I still did not pass:
$(function () { $('#autocomplete-ajax').autocomplete({ source: function(request, response){ $.ajax({ type: 'POST', dataType: 'json', url : '/institution/GetList', data:{ maxRows: 12, // показать первые 12 результатов nameStartsWith: request.term // поисковая фраза }, success: function(data){ response($.map(data, function(item){ return { plink: item.plink, // ссылка на страницу товара label: item.title_ru // наименование товара } })); } }); }, select: function( event, ui ) { // по выбору - перейти на страницу товара // Вы можете делать вывод результата на экран location.href = ui.item.plink; return false; }, minLength: 3 // начинать поиск с трех символов }); }); Handler:
public function GetList(){ $res = array(); if ($this->input->get('term')){ $result = $this->institutions_model->GetInstitutions(array('like' => $this->input->get('term'))); if (!empty($result)) { foreach ($result as $value) { $res[] = array('id' => $value->idMedicalFacilities, 'name' => $value->MedicalFacilitiesName); } } } echo json_encode($res); } Writes the following error in the console:
GET http://site.ru/user/null?query=%D0%BA 404 (Not Found)