Always get 'No results found' with ajax option

6,964 views
Skip to first unread message

hsp...@gmail.com

unread,
Jul 22, 2015, 1:06:09 PM7/22/15
to select2
I'm trying to dynamically load the select2 options using the ajax feature but I keep getting the 'No results found' error even though upon inspection of the json there are results. Really stumped on this. Here is my code and json:

Code:

<select id="filterUser" style="width:300px;">
<option value=""><option>
</select>

$("#filterUser").select2({
minimumInputLength: 2,
allowClear: true,
ajax: {
url: "/Home/SearchUser",
dataType: 'json',
data: function (params) {
return {
term: params.term
};
},
processResults: function (data) {
alert(data);
return {
results: data
};
},
results: function (data) {
return {
results: data
};
}
}
});


Json:

{
"results":[
{
"id":"987567",
"text":"Paredes, Mike"
},
{
"id":"432654",
"text":"Parker, Mike"
},
{
"id":"567987",
"text":"Parker, Mike"
},
{
"id":"345345",
"text":"Parisi, Mike"
},
{
"id":"454545",
"text":"Paredes Durval, Mike"
},
{
"id":"888888",
"text":"Parker, Mike"
},
{
"id":"666666",
"text":"Parker, Shondra"
},
{
"id":"555555",
"text":"Parzons, Jossie"
},
{
"id":"222222",
"text":"Paredes Jr, Mike"
},
{
"id":"111111",
"text":"Parra, Mike"
},
{
"id":"333333",
"text":"Paredes, Mike"
},
{
"id":"321321",
"text":"Park, Mike"
},
{
"id":"123123",
"text":"Parrish, Mike"
}
]
}


I'm using Select2 4.0.

Kevin Brown

unread,
Jul 22, 2015, 2:29:24 PM7/22/15
to select2
You are using `ajax.results` (Select2 3.x) and `ajax.processResults` (Select2 4.x), but you actually don't need any of them. Your JSON is coming back how Select2 is expecting it, and you should be able to remove both of these methods and see results coming back.

If you set `debug: true` when initializing Select2, it should complain about not receiving an array back in `results`, which is because you are nesting the `results` key twice.

--
You received this message because you are subscribed to the Google Groups "select2" group.
To unsubscribe from this group and stop receiving emails from it, send an email to select2+u...@googlegroups.com.
To post to this group, send email to sel...@googlegroups.com.
Visit this group at http://groups.google.com/group/select2.
For more options, visit https://groups.google.com/d/optout.

hsp...@gmail.com

unread,
Jul 22, 2015, 5:16:16 PM7/22/15
to select2, ke...@kevin-brown.com
I removed 'ajax.processResults' and 'ajax.results', but I am still not getting any results back. I got the error you mentioned about not receiving an array back in 'results', but I also got another one:

"Uncaught TypeError: Cannot read property 'slice' of undefined".

From what I've read online it seems the above error occurs when the json is not formatted correctly.

Can you show me how I am nesting the 'results' key twice?

Kevin Brown

unread,
Jul 22, 2015, 6:55:10 PM7/22/15
to hsp...@gmail.com, select2
The nesting was happening before because of the `ajax.processResults` method. It was converting your data into a nested representation under the key `results`, but your `data` already had that nested representation.

The warning about results being nested is because of some automated checks done by Select2 on the results [1], based on what is returned from `processResults` (which, by default, will be the data returned in your JSON response [2]).

- If nothing was returned in the response (`null` or a blank response)
- If nothing was returned in the `results` key (`null` or a missing key)
- If the `results` key wasn't an array

You can verify that you pass all of these checks by using `console.log` in `ajax.processResults` to ensure that your response is properly being parsed by jQuery. Check that you are not getting a string (instead of a JSON object) as this could be a sign of invalid JSON in your response.

hsp...@gmail.com

unread,
Jul 22, 2015, 7:22:04 PM7/22/15
to select2, ke...@kevin-brown.com
I was finally able to get it working. What I ended up doing was passing only the id and text field collection from my action method, and then in 'ajax.processResults' returned 'results: data'. What is weird is that the final json looked exactly the same.

Thank you for your help

$("#searchUser").select2({
placeholder: "Search by User",
debug: true,
minimumInputLength: 2,
allowClear: true,
delay: 250,
ajax: {
url: "/Home/SearchUser",
dataType: 'json',
data: function (params) {
return {
term: params.term
};
},
processResults: function (data) {
return {
results: data,
more: false
};
}
}
});

christian....@gmail.com

unread,
Oct 12, 2016, 3:25:08 PM10/12/16
to select2, ke...@kevin-brown.com, hsp...@gmail.com
Thank you, I just encountered the same problem!!
Reply all
Reply to author
Forward
0 new messages