JQuery select2 AJAX not working

I am using the jQuery select2 plugin and trying to get AJAX to work with my ext data that obviously doesn't work, and I'm just wondering if anyone can tell me what I'm doing wrong or something is missing?

NOTE. It is only for selection v3.5.2

my js:

$('#cliselect').select2({ ajax: { dataType: "json", url: "clientprojectpopulate.php", results: function (data) { return {results: data}; } } }); 

HTML:

 <select id="cliselect" name="cliselect" style="width: 100%;" /></select> 

my JSON is returned (which I believe is really):

 [{"id":"62","text":"Alberta Innovates Health Solutions"},{"id":"4","text":"Alterna Savins & Credit Union"},{"id":"63","text":"BC Patient Safety & Quality Council"}] 
+7
javascript jquery ajax jquery-select2
source share
2 answers

Found out because I used <select>

It must be <input> to load ajax data ...

 <input type="hidden" id="cliselect" name="cliselect" style="width: 100%;" /> 
+5
source share

Select2 has been updated to version 4.0. Now the input fields no longer work, and there should be a select element.

Results have been changed to

 processResults: function (data) { return { results: data }; } 

Inside the processResults function processResults you can use them as follows:

 processResults: function (data) { var results = []; $.each(data, function (index, account) { results.push({ id: account.AccountID, text: account.AccountName }); }); return { results: results }; } 
+10
source share

All Articles