Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

196
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda