简体   繁体   中英

406 (Not Acceptable) error on rails and jquery mobile autocomplete

I keep getting this 406 (Not Acceptable) error when i type in an autocomplete field.

I am using jquery mobile and jquery ui autocomplete and rails

here is my jquery

$("#request_artist").focus(function(){ 
    $("#request_artist").autocomplete({ 
        source: function(req, add){ 
            $.getJSON(artistRequestUrl, req, function(data) { 
                var suggestions = data.suggestions; 
                add(suggestions);    
            });   
        },
        change: function() {
            $("#request_song").attr("disabled", false); 
            $("#request_submit").attr("disabled", false); 
            $("#request_like_reason").attr("disabled", false); 
            $('.disable_color').css("color", "black");   
        },    
    });
});    

here is my html

<div data-role="fieldcontain">
    <label for="search">Artist</label>
    <input type="search" name="password" id="request_artist" value="" />
</div>

here is my rails action

def ajax_artist
  bands = Band.all
  artists = bands.map(&:name).uniq.sort
  filter_input(artists, params[:term])
  Rails.logger.info @all_instances_hash.inspect
  respond_to do |format|
    format.json { render :json => @all_instances_hash}
  end
end

if i look at the logger in the console i get this for @all_instances_hash if i enter the letter c

 {:query=>"c", :suggestions=>["Casting Crowns", "John Mark McMillan"]}

But the autocomplete is not working...any idea

I'd guess that Rails thinks you're asking for HTML back. Your ajax_artist can only ever respond (sensibly) with JSON so try ignoring the format completely:

def ajax_artist
  bands = Band.all
  artists = bands.map(&:name).uniq.sort
  filter_input(artists, params[:term])
  Rails.logger.info @all_instances_hash.inspect
  render :json => @all_instances_hash
end

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM