0

I am using the jquery-tokeninput and is not able to get it to work, the ajax call is not entering the success method when the result contains a list of results.

I use jQuery version 1.9.1 and the tokeninput version 1.6.1

I am using this html, and jquery script:

<input type="text" class="selector" name="users">

$('.selector').tokenInput("/event/usersearch", { method: 'POST', minChars: 2 });

When I test it I get the following response:

Click on the edit field to provide it with focus.

  • a dropdown is shown with the text "Type in a search term"

Press the letter u.

  • the dropdown disappears as expected as i set it to search after to chars

press the letter p.

  • the dropdown comes back with the text "Searching..."
  • a request is sent to the server with the query string "up"
  • the server respond with this (Content-Type: application/json;charset=utf-8)(55 bytes):

    [{id:2, name:'Superuser'},{id:3, name:'Bo Superduper'}]

    but the dropdown is still showing "Searching..."

press the letter x

  • a request is set to the server with the query string "upx"
  • the server responds with(2 bytes): []
  • the dropdown changes to "No results"

So when the server does return an empty list all works fine, but if records are returned the userinterface is not showing the items.

I tried with both GET and POST requests...

I tried to return [{id:2, name:"Superuser"},{id:3, name:"Bo Superduper"}] but with same result

I tried to return [{id:"2", name:"Superuser"},{id:"3", name:"Bo Superduper"}] but with same result

I tried to return [{"id":"2", "name":"Superuser"},{"id":"3", "name":"Bo Superduper"}] but with same result

Within the tokeninput java script file, I set a breakpoint at the first line in the ajax success function:

// Attach the success callback
ajax_params.success = function(results) {
cache.add(cache_key, $(input).data("settings").jsonContainer ? results[$(input).data("settings").jsonContainer] : results);

And it is not called on the requests that contains items, but the requests that returns an empty lists will be called. This explains why the "Searching..." remains on, and only changes when empty lists are returned.

All requests return http status 200 OK.

From the byte count the utf-8 is the same as ascii, so it shouldnt be a encoding problem...

So the ajax request is sent, and returns but the success method is not called?

1 Answers1

0

How are you currently returning your JSON? If the ajax call isn't calling success I'd guess your response is somehow malformed. For example, of the 4 versions you've tried - only the last one is valid. JSON requires double quotes, and also attribute names to be in quotes.

I'd suggest automatically generating your JSON string at source, which should ensure you get the correct header types and the like too. If it's php, you can use:

echo json_encode(//PHP Assoc Array Here);

Chris
  • 5,882
  • 2
  • 32
  • 57