Method PUT is not allowed by Access-Control-Allow-Methods

5.4k views Asked by At

I got an error in browser when using ajax to edit posts. XMLHttpRequest cannot load http://example.com/posts/100. Method PUT is not allowed by Access-Control-Allow-Methods.

In rails server:

  Started OPTIONS "/posts/100" for 127.0.0.1 at 2014-11-14 11:51:39 -0800
  Processing by ApplicationController#handle_options_request as */*
  Parameters: {"path"=>"posts/100"}

I went through several solution and did something like this in rotues.rb:

   match '*path', :controller => 'application', :action => 'handle_options_request', :constraints => {:method => 'OPTIONS'}

in application_controller.rb

  def handle_options_request
    headers['Access-Control-Allow-Origin'] = request.env['HTTP_ORIGIN']
    headers['Access-Control-Allow-Methods'] = 'POST, GET, OPTIONS'
    headers['Access-Control-Max-Age'] = '1000'
    headers['Access-Control-Allow-Headers'] = '*,x-requested-with'
    head(:ok) if request.request_method == "OPTIONS"
  end

Anyone has solution to get around it. Also try rack-cors, but it does not work

1

There are 1 answers

0
Alexandru R On

Try changing this line to (add PUT):

headers['Access-Control-Allow-Methods'] = 'POST, GET, OPTIONS, PUT'