【问题标题】:Including ActionController::Live breaks existing AJAX calls包括 ActionController::Live 会中断现有的 AJAX 调用
【发布时间】:2014-09-25 04:30:30
【问题描述】:

我有一个带有多个动作/视图的控制器,其中只有一个会利用ActionController::Live 模块。但是,一旦包含在内,AJAX 操作(在不相关的页面上)就根本不再呈现在客户端上。

以下代码可以正常运行:

my_controller.rb:

class MyController < ApplicationController
    def index
        // renders vanilla HTML/JS from index.html.erb
    end

    def update_index
        // renders JavaScript from index.js.erb
    end
end

index.html.erb:

<%= button_to({ controller: :my_controller, action: :update_index},
            remote: true,
            method: 'post') do %>
    Update the text
<% end %> 

<div id='content'>Some content</div>

update_index.js.erb:

$('#content').html('You clicked the button.');

问题: 一旦我将include ActionController::Live 添加到控制器,甚至在为服务器端事件创建任何JavaScript 或Rails 句柄(效果很好)之前,我现有的代码就会停止工作。发生以下情况:

  • 服务器看到传入的 AJAX 请求
  • 调用所有适当的控制器函数
  • JavaScript 不在客户端执行。

【问题讨论】:

    标签: javascript ruby-on-rails ajax html


    【解决方案1】:

    如果您在控制器中包含ActionController::Live,它似乎会更改返回客户端时的默认标头情况。在我的非 SSE 操作中添加以下行似乎可以解决问题:

    response.headers["Content-Type"] = 'text/javascript'
    

    但很想知道是否有更好的解决方案,或者我是否应该在所有适当的操作上这样做。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-09-29
      • 1970-01-01
      • 1970-01-01
      • 2017-02-16
      • 1970-01-01
      • 2015-01-25
      • 1970-01-01
      相关资源
      最近更新 更多