[英]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.