繁体   English   中英

如何在我的Rails RESTful Web服务中接受PUT和DELETE方法

[英]How to accept PUT and DELETE methods in my Rails RESTful web service

我已经构建了一个返回JSON的Ruby On Rails 3.0.x应用程序。 到目前为止,我的广告控制器中只有四种方法:index,show,update,destroy。

当我尝试从同一域内的应用程序(通过AJAX和jQuery)调用方法时,我成功了。 但是,当我尝试从另一个域中的另一个应用程序执行相同操作时,我收到以下错误消息,只有当我尝试使用方法PUT和DELETE(它适用于GET和POST):

XMLHttpRequest无法加载URL_HERE来源Access-Control-Allow-Origin不允许URL_HERE。

我的RESTful服务是通过HTTP调用的,而不是HTTPS。

下面是我正在使用的AJAX代码(16是广告的id):

$.ajax({
    type: "DELETE",
    url: "http://SERVICE_URL/advertisements/16.json",
    crossDomain: true,
    success: function(response){
            alert("test");

    }
    });

有任何想法吗?

谢谢。

你可以尝试机架式的 我们为我们的Web服务启用CORS:

# config/application.rb
config.middleware.use Rack::Cors do |requests|
  requests.allow do |allow|
    allow.origins '*'
    # perhaps you would stick :put and :delete here?
    # then you should follow the rack-cors documentation to only enable it where necessary
    allow.resource '*', :headers => :any, :methods => [:get, :post]
  end
end

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM