为了解决这个问题,Rails Apps
需要让对方访问或者说互相ping。为了做到这一点,我们必须使用CORS之类的东西来允许来自其他域的 AJAX 请求。这是我用来允许方法访问文章的一种更简单的方法,起初听起来可能令人困惑。
要在双方都执行此操作,您还需要为他们创建一个实际完整的应用程序,这Faye
与我在问题中附加的 Faye 聊天服务器 Repo 上给出的内容不同。
在实际/主应用程序中,将其包含在您的application_controller.rb
before_filter :cors_preflight_check
after_filter :cors_set_access_control_headers
#For all responses in this controller, return the CORS access control headers.
def cors_set_access_control_headers
headers['Access-Control-Allow-Origin'] = 'http://YOURFAYEAPP.com'
headers['Access-Control-Allow-Methods'] = 'POST, PUT, DELETE, GET, OPTIONS'
headers['Access-Control-Request-Method'] = 'http://YOURFAYEAPP.com'
headers['Access-Control-Allow-Headers'] = 'Origin, X-Requested-With, Content-Type, Accept, Authorization'
end
# If this is a preflight OPTIONS request, then short-circuit the
# request, return only the necessary headers and return an empty
# text/plain.
def cors_preflight_check
if request.method == :options
headers['Access-Control-Allow-Origin'] = 'http://YOURFAYEAPP.com'
headers['Access-Control-Allow-Methods'] = 'POST, PUT, DELETE, GET, OPTIONS'
headers['Access-Control-Request-Method'] = 'http://YOURFAYEAPP.com'
headers['Access-Control-Allow-Headers'] = 'Origin, X-Requested-With, Content-Type, Accept, Authorization'
render :text => '', :content_type => 'text/plain'
end
end
这样,两者都可以发送和接收请求。不要*
代替 request parameter
,这是一个安全问题。你可以减少methods
到只有Get
和Post
。