我的应用程序使用 Rails 5.1,每当我点击带有 remote: true
的 link_to
时,我都会收到 InvalidCrossOriginRequest
错误。关闭伪造保护后,应用程序最终会向服务器发出完整的 GET
请求并显示 javascript。
这是链接:
= link_to 'Example', example_url, remote: true
example.js.erb:
$('.example').html('alert("example message")');
路线:
scope :js, defaults: { format: :js } do
#### Example
get 'example', to: 'examples#example'
end
Controller :
def example
respond_to do |format|
format.html { redirect_to root_url, alert: 'Page not accessible' }
format.js
end
end
<小时/>
我在模板中包含了 javascript:
= javascript_include_tag 'application', 'data-turbolinks-track': 'reload'
我添加了必要的js资源路径:
//= require_self
// jQuery
//= require jquery
//= require jquery_ujs
最佳答案
您已在 Controller 中使用 respond_to
指定格式:
respond_to do |format|
format.js
end
关于javascript - Rails `remote: true` 未发出 AJAX 请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44653530/