【问题标题】:XMLHttpRequest No 'Access-Control-Allow-Origin' header From SimpleHttpServerXMLHttpRequest 没有来自 SimpleHttpServer 的“Access-Control-Allow-Origin”标头
【发布时间】: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


    【解决方案1】:

    原来问题是我将请求发送到 (http://localhost:3000/campaigns/my_campaigns) 的 url 不正确。它正在匹配我不打算匹配的操作。此操作触发了“缺少模板”异常。不知何故,这导致了 No 'Access-Control-Allow-Origin' 错误。不知道这是怎么发生的,但是一旦我修复了我的网址以进行预期的操作,一切都很好。

    【讨论】:

      猜你喜欢
      • 2016-01-21
      • 1970-01-01
      • 2019-08-15
      • 2022-12-02
      相关资源
      最近更新 更多