title: AngularJS and sinatra CORS
body: <p>I am trying to do a post using AngularJS</p>
  
  <pre><code>$http.post('http://localhost:9393/doRegister',$scope.newUser).success (data)=&gt;
    alert data
  </code></pre>
  
  <p>To a sinatra controller</p>
  
  <pre><code>    post '/doRegister' do
        data = request.body.read
        return data.password
      end
  </code></pre>
  
  <p>i used the following configuration for CORS </p>
  
  <pre><code>#CORS enable
  before do
    headers['Access-Control-Allow-Methods'] = 'GET, POST, PUT, DELETE, OPTIONS'
    headers['Access-Control-Allow-Origin'] = '*'
    headers['Access-Control-Allow-Headers'] = 'accept, authorization, origin'
  end
  
  options '*' do
    response.headers['Allow'] = 'HEAD,GET,PUT,DELETE,OPTIONS,POST'
    # Needed for AngularJS
    response.headers['Access-Control-Allow-Headers'] = 'X-Requested-With, X-HTTP-Method-Override, Content-Type, Cache-Control, Accept'
  end
  </code></pre>
  
  <p>but i still get the following error from chrome </p>
  
  <pre><code>XMLHttpRequest cannot load http://localhost:9393/doRegister. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:4567' is therefore not allowed access. 
  </code></pre>
  
  <p>when i try to debug the app the console shows:</p>
  
  <pre><code>W, [2013-11-20T01:39:01.778466 #3716]  WARN -- : attack prevented by Rack::Protection::HttpOrigin
  127.0.0.1 - - [20/Nov/2013 01:39:01] "POST /doRegister HTTP/1.1" 403 - 0.0016
  </code></pre>
  
  <p>i tried using the following, cors_enable gem, rack/cors gem i tried disabling rack protection using some options like the folowing </p>
  
  <pre><code>use Rack::Protection::HttpOrigin, :origin_whitelist =&gt; ['*']
  
  set :protection, :origin_whitelist =&gt; ['http://localhost:5445']
  set :protection, :except =&gt; :frame_options
  set :protection, :except =&gt; :json_csrf
  set :protection, :except =&gt; :http_origin
  </code></pre>
  
  <p>I am out of ideas, please advise.</p>