Reputation: 57
My function is:
jQuery.post("example.php",{}, function(data){$("#select2").html('').select2({data: data});});
and return json:
{"results":[{"id":"aasd23423d3d","text":"dfasie"},{"id":"asdf2fsdf","text":"velder"}"}]}
but select2 is filling in separating the letters from the words, like the picture
Upvotes: 2
Views: 1240
Reputation: 2475
Try:
<select id="select2"></select>
$('#select2').select2({
ajax: {
type: 'POST',
url: 'example.php',
data: {},
dataType: 'json'
processResults: function (data) {
return {
results: data.results
};
}
}
});
To know more, visit Select2 with Ajax
Upvotes: 2
Reputation: 171669
The last argument of jQuery.post is dataType
. Try setting it to 'json'
and the data will get parsed internally to an object for you.
It appears that currently jQuery is assuming it is just text coming from server
If you are in control of the php code you also should set content type header for application/json
jQuery.post("example.php",
{},
function(data) {
$("#select2").html('').select2({
data: data
});
},
'json'
);
Upvotes: 1