【问题标题】:Getting No 'Access-Control-Allow-Origin' with Swagger and Foreman Rails server使用 Swagger 和 Foreman Rails 服务器没有“访问控制允许来源”
【发布时间】:2017-11-03 13:11:21
【问题描述】:

我的服务器当前在 0.0.0.0:5100 运行。当我尝试访问我的 Swagger 文档时,我在控制台中收到以下错误:

XMLHttpRequest cannot load http://0.0.0.0/api/v1/types.json. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://0.0.0.0:5100' is therefore not allowed access. 

我想知道为什么会这样。这是我在 index.html 中的 Swagger 配置:

$(function () {
      window.swaggerUi = new SwaggerUi({
      url: "/api-docs.json",
      dom_id: "swagger-ui-container",
      supportedSubmitMethods: ['get', 'post', 'put', 'delete'],
      onComplete: function(swaggerApi, swaggerUi){
        log("Loaded SwaggerUI");

        if(typeof initOAuth == "function") {
          /*
          initOAuth({
            clientId: "your-client-id",
            realm: "your-realms",
            appName: "your-app-name"
          });
          */
        }
        $('pre code').each(function(i, e) {
          hljs.highlightBlock(e)
        });
      },
      onFailure: function(data) {
        log("Unable to Load SwaggerUI");
      },
      docExpansion: "none"
    });

【问题讨论】:

    标签: ruby-on-rails swagger


    【解决方案1】:

    您是否为您的网络服务启用了CORS 支持? The Swagger documentation 要求 CORS 支持才能正常工作。您收到的错误消息与我在添加 CORS 支持之前在 Grails 项目中收到的消息完全相同。

    【讨论】:

      【解决方案2】:

      Allow-Control-Allow-Origin: * chrome store 上的扩展已添加到浏览器中。

      注意:一旦您完成工作,请禁用此选项,因为 youtube 将无法使用启用选项。

      【讨论】:

        猜你喜欢
        • 2017-11-03
        • 2018-10-18
        • 2017-07-21
        • 2018-08-10
        • 2021-10-08
        • 2013-08-20
        • 2016-06-27
        • 2016-12-18
        相关资源
        最近更新 更多