Answer the question
In order to leave comments, you need to log in
jquery autocomplete giving error?
When the button is pressed, a function is initialized that sends a request to node.js express get, which in response sends an array with the names of cities, the array consists of 10969 elements ["Moscow", "Abramtsevo", "Alabino", "Aprelevka", "Arkhangelsk ", "Ashitkovo", "Baikonur", "Baksheyevo", "Balashikha", "Barybino", "Beloomut", "White Pillars", "Borodino", "Bronnitsy", "Bykovo", "Valuevo", "Verbilki" , "Vereya", "Vidnoe", "Vnukovo", "Leader of the Proletariat" ...].
var citiesList;
function loadCitiesNames() {
$.ajax({
url: "http://localhost:3000/cities/get",
type: 'GET',
crossDomain: true,
dataType: 'jsonp',
contentType: 'application/json; charset=utf-8',
success: function (data) {
citiesList = data;
console.log(citiesList);
},
error: function(request,msg,error){
console.log(request + ' ' + msg + ' ' + error);
}
});
}
router.get('/get', function(req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "X-Requested-With");
res.contentType('json');
res.setHeader('Content-Type', 'application/json');
var data = JSON.parse(fs.readFileSync('cities.json'),'utf8');
var cities = data.city;
var citiesNames = [];
for (var i = 0; i < cities.length; i++) {
citiesNames.push(cities[i].name);
}
res.jsonp(citiesNames);
});
module.exports = router;
$('#cityInput').autocomplete({source: citiesList, appendTo: this})
<div class="modal fade" id="weaModal" tabindex="-1" role="dialog" aria-labelledby="weaModalLabel" aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="weaModalLabel">Выберите город:</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<input id="cityInput" class="form-control" type="text" placeholder="Введите название города">
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Отмена</button>
<button type="button" class="btn btn-primary">Сохранить</button>
</div>
</div>
</div>
</div>
<script src="https://code.jquery.com/jquery-3.5.0.min.js" integrity="sha256-xNzN2a4ltkB44Mc/Jz3pT4iU1cmeR0FkXs4pru/JxaQ=" crossorigin="anonymous"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js" integrity="sha256-T0Vest3yCU7pafRw9r+settMBX6JkKN06dqBnpQ8d30=" crossorigin="anonymous"></script>
<link rel="stylesheet" type="text/css" href="./css/jquery-ui.css">
Answer the question
In order to leave comments, you need to log in
Answer found!!
In order for autocomplete to work, it was necessary to initialize it when a successful response came from the server
function loadCitiesNames() {
$.ajax({
url: "http://localhost:3000/cities/get",
type: 'GET',
crossDomain: true,
dataType: 'jsonp',
contentType: 'application/json; charset=utf-8',
success: function (data) {
setAutocomplite(data);
},
error: function(request,msg,error){
console.log(request + ' ' + msg + ' ' + error);
}
});
}
function setAutocomplite(data){
$('#cityInput').autocomplete({source: data, appendTo: $('#modalBody')})
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question