A
A
ArtGMlg2020-05-11 16:33:26
JavaScript
ArtGMlg, 2020-05-11 16:33:26

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);
    }
  });
}

This is what the server side looks like
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;


This array is then written to a variable, which serves as the source for autocomplete.
$('#cityInput').autocomplete({source: citiesList, appendTo: this})


The input itself is in the modal window that the button calls
<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">&times;</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>


When trying to enter something in the console, the following error occurs:
jquery-ui.js:6015
Uncaught TypeError: this.source is not a function
at $..._search (jquery-ui.js:6015)
at $.. ._search (jquery-ui.js:144)
at $...search (jquery-ui.js:6007)
at $...search (jquery-ui.js:144)
at $... (jquery-ui .js:5988)
at handlerProxy (jquery-ui.js:641)

Connected to the page:
<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

1 answer(s)
A
ArtGMlg, 2020-05-11
@ArtGMlg

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 question

Ask a Question

731 491 924 answers to any question