XMLHttpRequest否来自SimpleHttpServer的'Access-Control-Allow-Origin'标头

问题描述 投票:0回答:1

我正在编写Rails后端应用程序。我向单独工作的前端开发人员提供了一个REST 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向当前也在本地运行的后端,在http:// localhost:3000上发送一个ajax请求时,出现以下错误:

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个回答
0
投票

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

© www.soinside.com 2019 - 2024. All rights reserved.