gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 04:03:54 25 4
gpt4 key购买 nike

我的应用程序使用 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com