3

I have a feature spec, as part of the page load. I hit a local url to get more data.

i.e. using http://fullcalendar.io/ which loads it's events via ajax

   events: {
             url: 'calendar_events.json',
             type: 'GET',
             error: function(response) {
             ...
            }},

I am getting

XMLHttpRequest cannot load http://localhost:3000/calendar_events.json?start=2014-01-20&end=2014-01-27. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'null' is therefore not allowed access. The response had HTTP status code 404.

Not exactly sure how to resolve. I tried adding

    response.headers['Access-Control-Allow-Origin'] = '*'
    response.headers['Access-Control-Allow-Methods'] = 'POST, PUT, DELETE, GET, OPTIONS'
    response.headers['Access-Control-Request-Method'] = '*'
    response.headers['Access-Control-Allow-Headers'] = 'Origin, X-Requested-With, Content-Type, Accept, Authorization'

To a before_filter on my application controller just to see, but still get same issue.

NOTE: I am using capybara-webkit

dboyd68
  • 1,104
  • 15
  • 33
  • http://stackoverflow.com/a/6120260/3274024 you could check that out, maybe that is whats wrong with the request. – jfornoff May 12 '15 at 06:10
  • Don't really want to change it to jsonp – dboyd68 May 12 '15 at 23:32
  • Maybe the cross domain trigger is enough. Not sure if I understand why you are hitting the local url, could you not just render it right away on server side? – jfornoff May 13 '15 at 05:34

1 Answers1

2

The best way to set these would be in your test environment, as you wouldn't want to open up origins in production.

test.rb

  config.action_dispatch.default_headers.merge!(
  {
      'Access-Control-Allow-Origin' => '*',
      'Access-Control-Allow-Methods' => 'POST, PUT, DELETE, GET, OPTIONS',
      'Access-Control-Max-Age' => "1728000",
     'Access-Control-Allow-Headers' =>'Origin, X-Requested-With, Content-Type, Accept, Authorization'
  })

As for your issue I suspect you are getting this error after 'save_and_open_page' in which case it is simply a rendering error based on your capybara asset_path and not the actual problem with your test case.

Allan W Smith
  • 756
  • 6
  • 20