Ajax请求返回(406 Not Acceptable)

时间:2013-06-20 02:13:32

标签: ruby-on-rails ajax ruby-on-rails-3 ruby-on-rails-3.1 ruby-on-rails-3.2

在我的Rails应用程序中,javascript模板 profile.js.erb 不会被渲染。我收到错误代码406 Not Acceptable。此模板应在视图 star#profile 中附加部分内容。 Ajax请求是由jquery无限滚动插件完成的。这是我的代码

动作

 def profile
 @page=params[:page] ||1
 @videos=Video.all(:page=>@page)
 respond_to do |format|
 format.js
 format.html
  end
end

查看星标#个人资料

<div id="content">
<div class="post">
<%=render 'videos'%>
</div>

profile.js.erb

  $('div.post').append("<%=escape_javascript(render 'videos')%>");

路由

      match "stars/profile/:page"=> "stars#profile", :via => :get

登录控制台

Started GET "/stars/profile?page=2" for 127.0.0.1 at 2013-06-20 00:05:58 -0500
Processing by StarsController#profile as application/JavaScript
Parameters: {"page"=>"2"}
Completed 406 Not Acceptable in 65836ms

Ajax设置

   jQuery.ajaxSetup({ 
  'beforeSend': function(xhr, settings) {
   xhr.setRequestHeader("Accept", "application/javascript");
   var token=$('meta[name="csrf-token"]').attr('content');
   xhr.setRequestHeader('X-CSRF-Token',token );
   settings['dataType'] = "javascript";
   settings['contentType'] = "application/javascript";
    }

});

1 个答案:

答案 0 :(得分:0)

从日志中: GET“/ stars / profile?page = 2”在个人资料后不包含.js。因此rails不会在javascript处理它。

获取“/stars/profile.js?page=2”应该是电话..