Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

195
Visualizações
select2 does not show the results received from ajax

Where is the error?

My select2 box doesn't show results received from ajax call, it says - The results could not be loaded.

Here is my code:

HTML:

<select class="airportList form-control" name="From_1"> 
 <option disabled selected>Going from</option>
</select>

JS:

$(function() {
  $(".airportList").select2({
    minimumInputLength: 1,
    // data: [{id:'ADL',text:'Adelaide, Australia, ADL'},{id:'MEL',text:'Melbourne, Australia, MEL'}],
    ajax: {
      type: "GET",
      url: "data.php",
      dataType: 'json',
      delay: 250,
      data: function (params) {
        return {
          q: params.term, // search term
          page: params.page
        };
      },
      processResults: function (data) {
        return {
          results: data
        };
      }
    }
  });
});

data.php (that I receive):

[
{id:'ADL',text:'Adelaide, Australia, ADL'},
{id:'MEL',text:'Melbourne, Australia, MEL'}
]
over 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

Sorry, here is an updated answer:

processResults: function(data) {
  return {
    results: $.map(data, function(obj) {
      return {
        id: obj.id,
        text: obj.text
      };
    })
  };
}

This part is where you have the issue I think. See this example: http://jsfiddle.net/jes0wrka/

over 4 years ago · Santiago Trujillo Relatório

0

So the problem in my case was that the format of the JSON from data.php file was INCORRECT!!!:

[
{id:'ADL',text:'Adelaide, Australia, ADL'},
{id:'MEL',text:'Melbourne, Australia, MEL'}
]

The CORRECT JSON should be:

[
{"id":"ADL","text":"Adelaide, Australia, ADL"},
{"id":"MEL","text":"Melbourne, Australia, MEL"}
]
over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda