SyntaxError: Unexpected token u with rails3-jquery-autocomplete 1.0.11 and jquery-rails 2.2.1
bollard opened this issue · comments
After upgrading to rails3-jquery-autocomplete 1.0.11 (so i can upgrade to jQuery 1.9 in jquery-rails 2.2.1), i am getting the following error in the Chrome Developer console. Everything appears to work fine and the error is only logged once i click out of the input box.
Uncaught SyntaxError: Unexpected token u jquery.js:542
jQuery.extend.parseJSON jquery.js:542
e.autocomplete.change autocomplete-rails.js:17
$.Widget._trigger jquery.ui.widget.js:481
$.widget._change jquery.ui.autocomplete.js:477
(anonymous function) jquery.ui.widget.js:97
_on.blur jquery.ui.autocomplete.js:184
handlerProxy jquery.ui.widget.js:398
jQuery.event.dispatch jquery.js:3075
elemData.handle jquery.js:2751
StackOverflow suggests that "undefined" is being passed into parseJSON...?
+1
anyone have a solution to this problem?
This error is happening on https://github.com/crowdint/rails3-jquery-autocomplete/blob/master/lib/assets/javascripts/autocomplete-rails-uncompressed.js#L76
var update_elements = jQuery.parseJSON(jQuery(this).attr("data-update-elements"));
Not using :update_elements results in this error.
Workaround: Add an empty :update_elements to your autocomplete_field.
ie.
f.autocomplete_field :brand_name, autocomplete_brand_name_products_path, :update_elements => {}
Fix in this lib would be adding a condition to check for existence of data-update-elements attribute.
I had the problem on https://github.com/crowdint/rails3-jquery-autocomplete/blob/master/lib/assets/javascripts/autocomplete-rails-uncompressed.js#L72
The problem seems to be that if data-id-element or data-update-elements are not explicitly defined with an empty string or empty hash respectively, they return "undefined", which is not being handled. It looks like the assumption is that they will return an empty string if not defined, which is obviously no longer the case.
Workaround:
f.autocomplete_field :brand_name, autocomplete_brand_name_products_path, :id_element => '', :update_elements => {}
+1
+1
+1