2017-05-10 34 views
0

我是新来的招摇,我试图做一个非常简单的规范,只有一个get方法来检索一个网页,这是代码:简单的招摇规范,检索一个HTML网页

{ 
    "swagger": "2.0", 
    "info": { 
    "title": "example", 
    "description": "Sample api to retrieve a web page.", 
    "version": "0.1" 
    }, 
    "host":"example.org", #"localhost:8080", 
    "schemes": [ 
    "https" 
    ], 
    "paths": { 
    "/":{ 
     "get":{ 
     "summary":"Return the web page.", 
     "description":"", 
     "produces":["text/html"], 
     "responses":{ 
      "200":{ 
      "description":"OK", 
      }, 
       "400":{ 
       "description":"Bad request" 
       }, 
       "404":{ 
       "description":"Not Found" 
       } 
      } 
      } 
     } 
    } 
} 

我正在使用swagger在线编辑器。

不幸的是,当我执行请求,但没有返回网页,我在规范已实施的状态代码的任何一个,这回我在细节部分中的错误:

TypeError: Failed to fetch

有人可以告诉我我错在哪里?

谢谢。

回答

1

对于“试用”按钮在中工作,您的API端点必须为CORS -enabled。也就是说,您的服务器(example.orglocalhost:8080)必须配置为返回某些响应标头,以允许editor.swagger.io向您的服务器发出跨域请求。这在这里有更详细的解释:
https://github.com/swagger-api/swagger-ui#cors-support

配置CORS的方式取决于用于托管应用程序的服务器/框架。此页面有一些常见的Web服务器的说明:
https://enable-cors.org/server.html

+0

谢谢,我已遵循您的建议,现在它的工作原理! – SimCor

+0

是否可以禁用“试试按钮”的CORS? – SimCor

+0

@SimCor:CORS被浏览器强制执行作为安全措施,所以它不能在服务器端或网页上禁用。某些浏览器提供了一种禁用CORS进行临时测试的方法 - 这只会影响浏览器_。例如,如果您使用Chrome以下列参数启动它:'chrome.exe --disable-web-security --user-data-dir = '。 – Helen