javascript - Rails `remote: true` 未发出 AJAX 请求

标签 javascript jquery ruby-on-rails ajax ruby-on-rails-5

我的应用程序使用 Rails 5.1,每当我点击带有 remote: truelink_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/

相关文章:

javascript - 来自 JQuery 的奇怪消息 - 假设没有找到它

javascript - Twitter Bootstrap 警报更改类

jquery - 无限轮播适用于 chrome 但不适用于 firefox

javascript - 如何根据表单值更改div id名称?

javascript - jquery if语句并删除iframe

css - SASS 上的引用图像

ruby-on-rails - 运行抽佣 Assets :precompile before tests that include :js tag and skip otherwise

javascript - 进行3次统一检查

javascript - 在 CSS slider 上添加自动播放

ruby-on-rails - 在 Rails/Cloudfront/Heroku 中设置 Assets 缓存过期