1

I'm trying to access a Heroku app that I'm running at

young-dusk-4185.herokuapp.com

However, when trying to access the response on this link, I get the No 'Access-Control-Allow-Origin headers present` error. Referring to another question, I've added

response.headers['Access-Control-Allow-Origin'] = '*' to my Ruby file as follows:

require 'sinatra'
require './test.rb'

get '/'  do
    result = FooRunner.run!

     File.open('output.json','w') do |f|
        f.write result
     end

    response.headers['Access-Control-Allow-Origin'] = '*'
    content_type :json
    result
end

When I tested it on fiddle, http://jsfiddle.net/Newtt/wF3ca/1/, the call fails. In the current fiddle method is GET. I've even tried with POST but that fails as well. The fiddle code was taken from this question:

How do I send a cross-domain POST request via JavaScript?

Any idea how to acquire the JSON response from the app? Any code examples will be appreciated. Thank you!

Community
  • 1
  • 1
Newtt
  • 6,050
  • 13
  • 68
  • 106

2 Answers2

2

This is not a Sinatra issue. You need to pass the data as a JavaScript object, not as string, i.e.:

data: {"some":"json"}

instead of

data: '{"some":"json"}'

Working example here: http://jsfiddle.net/6q5kP/2/

Patrick Oscity
  • 53,604
  • 17
  • 144
  • 168
0

Just to clarify p11y, Chrome complains about cross-origin errors under many circumstances; especially if your response is malformed. Try to return valid JSON after you enable the correct access control headers.

Edited: Check to make sure your server is not throwing an exception. The exception page will not be handled with the CORS mechanism enabled.

seo
  • 1,959
  • 24
  • 18