【发布时间】:2013-04-15 02:26:36
【问题描述】:
我的 ajax 回调没有在我的生产环境中触发。但是,它们在没有错误的情况下在开发中触发。为了讨论,我会简化一些事情。
假设我有一个使用remote: true的链接:
<%= link_to "Add Foo", new_foo_path, remote: true, id: 'new-foo' %>
foos_controller.rb
class FoosController < ApplicationController
def new
@foo = Foo.new
render partial: 'form' if request.xhr?
end
end
使用 Chrome 的控制台,我绑定到ajax:success:
$(document).on("ajax:success", "#new-foo", function() { console.log("success!"); });
在开发中这工作正常;我得到了“成功!” Chrome 控制台中的消息。
但是,在生产中,它没有。正在发出请求,响应为 form 部分,但回调未触发。
有什么想法吗?
PS。以下也不起作用。
$("#new-foo").bind("ajax:success", function() { console.log("success!"); })
config/environments/production.rb 没有变化。
编辑:事实证明,当我在生产环境中单击链接时,会触发 ajax:error。
$(document).on("ajax:error", "#new-foo", function() { console.log("error"); });
我的生产日志没有显示任何错误,Chrome 开发者工具中的网络标签显示 200 OK 响应,其中部分为正文。
但是,Content-Type 标头在生产中是 text/javascript,而在开发中是 text/html。 为什么相同的代码在生产中响应 text/javascript?
【问题讨论】:
标签: ruby-on-rails ajax