按照这个问题
How to set access-control-allow-origin in webrick under rails?,我可以从localhost获取并发布到localhost:3000.
但是,DELETE和PUT发生错误
这是我允许跨域访问的方式
class ApplicationController < ActionController::Base protect_from_forgery before_filter :allow_cross_domain_access def allow_cross_domain_access response.headers["Access-Control-Allow-Origin"] = "*" response.headers["Access-Control-Allow-Methods"] = "*" end end
知道怎么解决吗?
解决方法
*不是Access-Control-Allow-Methods响应头的有效值.您需要列出实际方法:
response.headers["Access-Control-Allow-Methods"] = "GET,PUT,POST,DELETE"
此外,如果您的请求有任何自定义请求标头,您还需要列出这些标头:
response.headers["Access-Control-Allow-Headers"] = "Content-Type,X-Requested-With"
最后请注意,您的控制器应该允许OPTIONS http请求.这是为了允许进行PUT或DELETE请求时所需的CORS预检请求.