How to add “Access-Control-Allow-Origin” headers to API Response in Ruby

前端 未结 1 1412
天命终不由人
天命终不由人 2021-01-25 21:15

I\'m interviewing for a front-end developer job and have been given a coding test to build a simple front-end interface. I\'ve been given the server, which has been written in

1条回答
  •  暖寄归人
    2021-01-25 22:03

    Sinatra is a simple and lightweight web server. The general idea is that you write response routes like this:

    get '/api' do
     "Hello world"
    end
    

    When you make a HTTP GET request to yoursite.com/api you will get a "Hello world" as response.

    Now to add the header you want, this should do the trick:

    get '/api' do
      response['Access-Control-Allow-Origin'] = '*'
      "Hello world"
    end
    

    0 讨论(0)
提交回复
热议问题