我的服务器当前运行在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"
    });

最佳答案

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

07-25 23:12