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

268
Views
Jquery select2 error while taking json data

I'm using jquery Select2 4.1.0-rc.0 This is my Javascitp code:

        $('#task_dname').select2({
        placeholder: 'Select an item',
//      tags: true,
//      cache: true,
        delay: 250,
        maximumInputLength: 20,
        minimumInputLength: 2,
        dataType: 'json',
        ajax: {
            type: 'post',
            url: 'query_list_tasks.php',
            data: function (params) {var post_data = {searching: params.term, query: 'task_list'}; return post_data;},
            processResults: function (data) {
                console.log(data);
            return {
              results: data.items
            };
          }
        }
      });;

PHP generating json:

while ($row = $result->fetch_assoc()) {
   $response[] =  array(
      "id" => $row['id'],
      "text" => $row['text']
   );
}

//echo json_encode($response);
echo '{ "results": '.json_encode($response).'}';

Json for processResults data (console.log):

{ "results": [{"id":"10","text":"Test1"},{"id":"11","text":"Test2"}]}

This seems ok but i get this error when i try to search something, and there is no options in select field showing up:

jquery.min.js:2 jQuery.Deferred exception: Cannot read properties of undefined (reading 'slice') TypeError: Cannot read properties of undefined (reading 'slice')

Something is missing or im just to stupid? I'm. stuck.

about 4 years ago · Juan Pablo Isaza
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!