开发者

Rails and jQuery producing 406 error

开发者 https://www.devze.com 2022-12-18 05:15 出处:网络
I am getting406 Not Acceptable error when submitting my form with jQuery. I have tried the common fix found on the web:

I am getting 406 Not Acceptable error when submitting my form with jQuery. I have tried the common fix found on the web:

jQuery.ajaxSetup({ 
  'beforeSend': function(xhr) {
    xhr.setRequestHeader("Accept", "text/javascript");
  }
}) 

This hasn't fixed the problem. request.format outputs text/html. I have been racking my brain all day trying to fix this. Rails 2.3.4, jQuery 1.3.2 and jQTouch.

Here is my jquery code:

jQuery(function () {
  $("#search").submit(function (e) {
    var $form = $(this);
    return app.search($form);
  });
});

var app = {
  search:function ($form) {
    $.ajax({

      type:$form.attr("method"), url:$form.attr("action"),
      dataType:"script", data:$form.serialize(),
      complete:function (req) {
        if (req.status === 200 || req.status === 304) {
        } else {
          alert("There was an error searching. Try again.");
          console.log(this);
        }
      }
    });    
    return false;
  }
};

The form:

  %form{:action => "/search", :method => "post", :id => "search", "accept-charset" => "utf-8"}
    %div{:style =>"margin:0;padding:0;display:inline" }
      %input{:name => "_method", :type => "hidden", :value => "put"}
      %input{:name => "authenticity_token", :type => "hidden", :value => "#{form_authenticity_token}"}
    %ul.rounded
      %li
        %input{:name => 'query', :value => '', :type => 'text', :placeholder => "Search"}

The controller code:

  def show
    query = params[:query]
    @results = Person.search(query)
    respond_to do |wants|
      wants.html { }
      wants.js 
    end   
  end

The show action is the one called. I have verified that, here is the log

Processing PeopleController#show (for 127.0.0.1 at 2010-01-28 14:12:19) [PUT]
  Parameters: {"authenticity_token"=>"y4kC2CZ1dnrV7LWEC2mRxv8mP499C+0HXbRVlDEuWDc=", 开发者_开发技巧"query"=>"purcell"}
  SQL (1.3ms)    SELECT trigger_name
 FROM all_triggers
 WHERE owner = 'PDSADMIN'
 AND trigger_name = 'PERSON_PKT'
 AND table_owner = 'PDSADMIN'
 AND table_name = 'PERSON'
 AND status = 'ENABLED'

  Person Columns (20.9ms)    select column_name as name, data_type as sql_type, data_default, nullable,
 decode(data_type, 'NUMBER', data_precision,
 'FLOAT', data_precision,
 'VARCHAR2', decode(char_used, 'C', char_length, data_length),
 'CHAR', decode(char_used, 'C', char_length, data_length),
 null) as limit,
 decode(data_type, 'NUMBER', data_scale, null) as scale
 from all_tab_columns
 where owner = 'PDSADMIN'
 and table_name = 'PERSON'
 order by column_id

  Person Load (3.7ms)   SELECT * FROM person WHERE (person.person_id IN (5554,55,5966,146)) 
Completed in 77ms (View: 20, DB: 26) | 406 Not Acceptable [http://localhost/search]


Ok, I found my problem. I was using the jqtouch plugin for rails that automatically assigns an :iphone format to requests in development mode, which forced the format.iphone which I didn't have. I used this code to override that:

if request.xhr? && request.format == :iphone
  request.format = :js
end


The correct way would be to edit (or add) config/initializers/mime_types.rb to accept :iphone requests.

Mime::Type.register_alias "text/javascript", :iphone
0

精彩评论

暂无评论...
验证码 换一张
取 消