Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

189
Views
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'}
]
about 4 years ago · Santiago Trujillo
2 answers
Answer question

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/

about 4 years ago · Santiago Trujillo Report

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"}
]
about 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!