Access-Control-Allow-Methods不允许使用方法PUT

时间:2014-11-14 19:59:57

标签: ruby-on-rails ruby ajax cross-domain-policy

使用ajax编辑帖子时,我在浏览器中出错。     XMLHttpRequest无法加载http://example.com/posts/100。 Access-Control-Allow-Methods不允许使用方法PUT。

在rails服务器中:

  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"}

我经历了几个解决方案并做了类似的事情 在rotues.rb中:

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

在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

任何人都有解决方案来解决它。也尝试使用rack-cors,但它不起作用

1 个答案:

答案 0 :(得分:2)

尝试将此行更改为(添加PUT):

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