首页 文章

Ajax请求返回(406 Not Acceptable)

提问于
浏览
1

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

.

action

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

view Stars#profile

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

profile.js.erb

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

routes

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

log in console

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 setup

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

    从日志中:GET“/ stars / profile?page = 2”在 Profiles 后不包含.js . 因此rails不能在javascript处理它 .

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

相关问题