我正在使用javascript_include_tag :defaults
的jquery-rails gem。因此页面有标题:
<script src="/javascripts/jquery.js?1312437063" type="text/javascript"></script>
<script src="/javascripts/jquery_ujs.js?1312437070" type="text/javascript"></script>
<script src="/javascripts/application.js?1311268601" type="text/javascript"></script>
<meta name="csrf-param" content="authenticity_token"/>
<meta name="csrf-token" content="KdgrBxF726xfPP3qbNOHd/0TG9c7lVCoZDXPnadFOzI="/>
Ajax请求来自视图行:
<%= link_to 'Run', :controller => "config_panel", :action => "full_run", :remote => true, :format => :js %>
它呈现为:
<a href="/config_panel/full_run.js?remote=true">Run</a>
控制器中定义的响应:
def full_run
respond_to do |format|
format.js { render :text => "alert();"}
format.xml
end
end
点击链接后,服务器会记录:
Started GET "/config_panel/full_run.js?remote=true"
Processing by ConfigPanelController#full_run as JS
Parameters: {"remote"=>"true"}
Rendered text template (0.0ms)
Completed 200 OK in 1ms (Views: 0.6ms | ActiveRecord: 0.0ms)
浏览器显示纯文本:
alert();
HTML标头包含status: 200
和content-type: text/javascript
。 Chrome JavaScript控制台没有显示任何错误。
我尝试了以下解决方法:
link_to
更改为<div onclick="$.ajax(...)">
---工作我对:remote =>
中link_to
的行为感到非常困惑。
答案 0 :(得分:0)
试试这个:
<%= link_to 'Run', { :controller => "config_panel", :action => "full_run" }, :remote => true %>
如果您使用了路由助手,则无需将其作为哈希传递