My app uses Rails 5.1 and whenever I click on a link_to
with remote: true
I get an InvalidCrossOriginRequest
error. After turning off forgery protection the app ends up making a full GET
request to the server and displays the javascript.
Here is the link:
= link_to 'Example', example_url, remote: true
example.js.erb:
$('.example').html('alert("example message")');
Route:
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
I included the javascript in my template:
= javascript_include_tag 'application', 'data-turbolinks-track': 'reload'
And I added the necessary js asset paths:
//= require_self
// jQuery
//= require jquery
//= require jquery_ujs
You have specify the format with respond_to
in your controller :
respond_to do |format|
format.js
end
问题原来是我正在运行的一段JavaScript:
e.stopPropagation();
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.