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

Asked 1 months ago, Updated 1 months ago, 6 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.

Status

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.
[Problem]
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
chrome
↓ Firefox Error
firefox

[cors.rb]

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

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

[routes.rb]

Rails.application.routes.drawdo
  get "contact/test"
end

constonClickSubmitBtnTest=()=>{
    const submit_btn = document.getElementById('btn_submit_test');
        var request = new XMLHttpRequest();
        request.open('GET', 'http://127.0.0.1:3000/contact/test', 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
        request.send();
}
<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.

https://github.com/cyu/rack-cors#rails-configuration
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


© 2022 OneMinuteCode. All rights reserved.