0

given some client side js

(function(){
  var submitEmail = function () {
    $("#join").click(function(e){
      var email = $("#email").val()
      if ( email.length === 0) { return false}
      console.log(email)
      $.ajax({
        crossDomain: true,
        type: "POST",
        url: "https://www.rubyonrailstutor.com/join.json",
        data: {first_name: "hacker", last_name: "github.io", email: "email" },
        dataType: "JSON"
      })
    });
  }
  submitEmail()

given the following config in a rails application controller

class ApplicationController < ActionController::Base
  protect_from_forgery

  before_filter :set_headers

  private

  def set_headers
    headers['Access-Control-Allow-Origin'] = 'http://rubyonrailstutor.github.io'
    headers['Access-Control-Allow-Methods'] = 'POST'
    headers['Access-Control-Allow-Headers'] = '*,x-requested-with,Content-Type,If-Modified-Since,If-None-Match,Auth-User-Token'
    headers['Access-Control-Expose-Headers'] = 'ETag'
  end
end

getting the following behavior on heroku

2014-03-07T18:17:34.258456+00:00 app[web.1]: Started POST "/join.json" for 99.108.137.170 at 2014-03-07 18:17:34 +0000
2014-03-07T18:17:34.363521+00:00 heroku[router]: at=info method=POST path=/join.json host=www.rubyonrailstutor.com request_id=96ce40a0-652a-4d40-ab29-a364e47d4815 fwd="99.108.137.170" dyno=web.1 connect=3ms service=113ms status=200 bytes=897
2014-03-07T18:18:14.482579+00:00 app[web.1]: Started POST "/join.json" for 99.108.137.170 at 2014-03-07 18:18:14 +0000
2014-03-07T18:18:14.491328+00:00 heroku[router]: at=info method=POST path=/join.json host=www.rubyonrailstutor.com request_id=cdb3e5dd-b200-4aa4-9fd2-c97688f64f58 fwd="99.108.137.170" dyno=web.1 connect=2ms service=12ms status=200 bytes=897
2014-03-07T18:20:38.956777+00:00 app[web.1]: Started POST "/join.json" for 99.108.137.170 at 2014-03-07 18:20:38 +0000

it seems like not enough data is being passed to the server, ie, there are data params and the server doesn't appear to be processing anything, the request is being made and then I can't tell what is happening, what should I be digging into to fix this ?

THanks !

John
  • 1,246
  • 15
  • 34

1 Answers1

1

You should use the rack-cors gem, it enables you to do so easily.

Rack::Cors provides support for Cross-Origin Resource Sharing (CORS) for Rack compatible web applications. The CORS spec allows web applications to make cross domain AJAX calls

Gemfile.rb:

gem 'rack-cors',
  :require => 'rack/cors'

config/application.rb

module Sample
  class Application < Rails::Application
    # other application config

    config.middleware.use Rack::Cors do
      allow do
        origins 'https://www.rubyonrailstutor.com/'
        resource %r{/users/\d+.json},
          :headers => ['Origin', 'Accept', 'Content-Type'],
          :methods => [:put, :delete]
      end
    end
  end
end

You can find a more detailed use here.

sidney
  • 2,704
  • 3
  • 28
  • 44
  • seems like http://stackoverflow.com/questions/19939207/heroku-rails-4-and-rackcors is a good follow on to this q – John Mar 08 '14 at 01:37
  • sure, the more important is for you to bypass your problem. – sidney Mar 08 '14 at 10:10
  • between, I just checked the link, and my answer was still correct. That's how stackoverflow works you know, you sure can find more detailed answers, but it is not a reason to undo validation. – sidney Mar 08 '14 at 19:56
  • ahh yeah, sorry that was my bad. the other answer was good and i really appreciated your post. – John Mar 10 '14 at 17:57