I want to communicate cross-origin in Ruby on Rails api mode.

Asked 5 months ago, Updated 5 months ago, 28 views

I'm studying ruby on rails, and I don't know much about it, but I'd appreciate it if someone could tell me.
Thank you in advance.


We are trying to introduce a contact form to a website that we have already published.
The front side uses html, javascript, and apache.
I would like to send emails using rails in api mode for the backend.
I installed a gem called "rack-cors" in rails and set up CORS, but
There is an error as shown in the picture below.
Chrome is not communicating and
if tested with Firefox The communication is working, but there is an error.
If anyone knows how to solve this problem, please let me know.
↓ chrome error
↓ Firefox Error


Rails.application.config.middleware.insert_before0, Rack::Cors do
  allow do

    resource "*",
      headers: —any,
      methods: [:get,:post,:put,:patch,:delete,:options,:head]


  get "contact/test"

    const submit_btn = document.getElementById('btn_submit_test');
        var request = new XMLHttpRequest();
        request.open('GET', '', true);

        // // Describes what to do when a response is returned
        // request.onload=function(){
        // // What to do when a response is returned
        // }
        // Send Request to URL
<button id="btn_submit_test" class="btn_submit" onclick={onClickSubmitBtnTest()}>Test<ic class=">/i><</button>

javascript ruby-on-rails ruby apache cors

2022-09-30 11:33

1 Answers

Try adding a description such as Rails.application.config.hosts<"example.com" to config/application.rb.The "example.com" part is the domain where the front end is running.

Here's what it says.

If it doesn't work, the cors.rb file might be misplaced, and it should have been loaded under config/initializers.

2022-09-30 11:33

If you have any answers or tips

© 2023 OneMinuteCode. All rights reserved.