【发布时间】:2014-12-17 03:11:41
【问题描述】:
我正在编写一个 Rails 后端应用程序。我正在为单独工作的前端开发人员提供休息 api。因此,我暂时通过在 application.rb 中添加以下内容来启用跨源资源共享:
#todo remove this once ui is integrated into the app. following allows requests from other domains (disble CORS).
config.action_dispatch.default_headers.merge!({
'Access-Control-Allow-Origin' => '*',
'Access-Control-Request-Method' => '*'
});
我正在尝试通过使用一个简单的 HTML 页面上的链接来测试 api,该页面是从 python SimplHttpServer 运行的。该页面在 http://localhost:8000/TestPage.html
上提供服务当我测试其中一个链接时(它使用 jquery 向后端发送 ajax 请求,该后端目前也在本地运行,在 http://localhost:3000 上),我收到以下错误:
GET http://localhost:3000/campaigns/my_campaigns?user_email=swapna%40urbancoding.net&user_token=SNa2kPqkm5ENsZMx7yEi
XMLHttpRequest cannot load http://localhost:3000/campaigns/my_campaigns?user_email=xyz.xyz.com&user_token=xyz. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost : 8000' is therefore not allowed access. The response had HTTP status code 500.
发生了什么事?我该如何解决这个问题?
【问题讨论】:
标签: ruby-on-rails-4 simplehttpserver