<?php
header('Content-Type: application/json');
$ret_string="{ results: [ {id:'1', text:'Option 1'}, {id:'2', text:'Option 2'} ],more:false}";
echo json_encode($ret_string);
Even this simple script returns nothing, so I'm not sure what I am doing wrong?Thanks in advance..Mac
$(document).ready(function() {
$('#selectbox-o').select2({
ajax: {
url: "mydata.php",
dataType: 'json',
delay: 250,
data: function (params) {
return {
q: params.term, // search term
page: params.page
};
},
processResults: function (data, page) {
return {
results: data.items
};
}
},
minimumInputLength: 3
});
})
Hi,
Can anyone either help me or point me in the right direction for the documentation on the format of the response back from the server using a remote data AJAX call?
It would appear that my php script is being executed but the Select2 option box never gets populated (the "searching" label does appear & disappear as you type).
Based on the topics on this group, I've changed my php script to be as simple as possible just to get things working:
<?php header('Content-Type: application/json');$ret_string="{ results: [ {id:'1', text:'Option 1'}, {id:'2', text:'Option 2'} ],more:false}"; echo json_encode($ret_string);
So I removed the json encoding and the output from the script is now
{"results":[{"id":0,"text":"text name0"}]}
which is a valid json format however I'm still not getting any results. The console for the Select2 gives me 2 errors:
Select2: The AJAX results did not return an array in the `results` key of the response.select2.js:3117 (anonymous function)Uncaught TypeError: Cannot read property 'slice' of undefined select2.js:3613
I think my example is as simple as it comes, so I've obviously missed something fundamental!
When you say AJAX example, do you mean the one on the Select2 examples page?
This example formats the response, whereas I just wanted a simple list. Do I still need to format the response with the templateSelection and templateResults?