为什么 Swagger UI 中有 "no response from server"?
Why is there "no response from server" in Swagger UI?
我的 API 调用在 Postman 中正常工作。但是当我从 Swagger UI 发送请求时,它显示所有请求的 "no response from server":
Response Body
no content
Response Code
0
Response Headers
{
"error": "no response from server"
}
可能是什么问题以及如何解决?
浏览器控制台显示这些错误:
Failed to load resource: net::ERR_CONNECTION_REFUSED
Uncaught TypeError: Cannot read property 'length' of undefined
at showStatus (index.js:24)
at showErrorStatus (index.js:24)
at error (index.js:607) at spec-converter.js:533
at Request.callback (index.js:24)
at Request.crossDomainError (index.js:24)
at XMLHttpRequest.xhr.onreadystatechange (index.js:24)
net::ERR_CONNECTION_REFUSED
听起来您需要在本地主机上启用 CORS,以便它在响应中发送 Access-Control-Allow-Origin: *
header。如何执行此操作取决于您使用的服务器。更多信息在这里:
https://enable-cors.org/server.html
https://github.com/swagger-api/swagger-ui/#cors-support
您可能还需要允许 OPTIONS pre-flight requests。
Swagger returns 由于序列化程序响应中的引用循环导致 0 响应代码。
在获取序列化程序响应时忽略引用循环。
如果您使用的是 Web API,请使用以下代码
services.AddMvc()
.AddJsonOptions(opt =>
{
opt.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore;
});
从 swagger 配置 json 文件中删除方案(在我的例子中 tsoa.json)并重新启动服务器。它对我有用。
{
"readme": "https://github.com/lukeautry/tsoa/blob/HEAD/tsoa.json",
"swagger": {
"outputDirectory": "./dist/src",
"entryFile": "./src/server.ts",
"basePath": "/",
"schemes": [
"http",
"https"
],
"securityDefinitions": {
"basic": {
"type": "basic"
}
}
},
"routes": {
"basePath": "/",
"entryFile": "./src/server.ts",
"routesDir": "./src",
"authenticationModule": "./src/security/Authentication"
}
}
就我而言,https 存在问题。在 swagger 配置中,http 方案是 "disabled"(不可用)。我是这样的:
GlobalConfiguration.Configuration.EnableSwagger(c => { c.Schemes(new[] { "https" }); });
而且我必须更改它才能在本地主机上进行调试:
GlobalConfiguration.Configuration.EnableSwagger(c => { c.Schemes(new[] { "https", "http" }); });
第一个片段在启用 https 的情况下在生产环境中运行,但在默认配置 Visual Studio 中调试时 id 不工作。
我的 API 调用在 Postman 中正常工作。但是当我从 Swagger UI 发送请求时,它显示所有请求的 "no response from server":
Response Body
no contentResponse Code
0Response Headers
{ "error": "no response from server" }
可能是什么问题以及如何解决?
浏览器控制台显示这些错误:
Failed to load resource: net::ERR_CONNECTION_REFUSED
Uncaught TypeError: Cannot read property 'length' of undefined
at showStatus (index.js:24)
at showErrorStatus (index.js:24)
at error (index.js:607) at spec-converter.js:533
at Request.callback (index.js:24)
at Request.crossDomainError (index.js:24)
at XMLHttpRequest.xhr.onreadystatechange (index.js:24)
net::ERR_CONNECTION_REFUSED
听起来您需要在本地主机上启用 CORS,以便它在响应中发送 Access-Control-Allow-Origin: *
header。如何执行此操作取决于您使用的服务器。更多信息在这里:
https://enable-cors.org/server.html
https://github.com/swagger-api/swagger-ui/#cors-support
您可能还需要允许 OPTIONS pre-flight requests。
Swagger returns 由于序列化程序响应中的引用循环导致 0 响应代码。
在获取序列化程序响应时忽略引用循环。
如果您使用的是 Web API,请使用以下代码
services.AddMvc()
.AddJsonOptions(opt =>
{
opt.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore;
});
从 swagger 配置 json 文件中删除方案(在我的例子中 tsoa.json)并重新启动服务器。它对我有用。
{
"readme": "https://github.com/lukeautry/tsoa/blob/HEAD/tsoa.json",
"swagger": {
"outputDirectory": "./dist/src",
"entryFile": "./src/server.ts",
"basePath": "/",
"schemes": [
"http",
"https"
],
"securityDefinitions": {
"basic": {
"type": "basic"
}
}
},
"routes": {
"basePath": "/",
"entryFile": "./src/server.ts",
"routesDir": "./src",
"authenticationModule": "./src/security/Authentication"
}
}
就我而言,https 存在问题。在 swagger 配置中,http 方案是 "disabled"(不可用)。我是这样的:
GlobalConfiguration.Configuration.EnableSwagger(c => { c.Schemes(new[] { "https" }); });
而且我必须更改它才能在本地主机上进行调试:
GlobalConfiguration.Configuration.EnableSwagger(c => { c.Schemes(new[] { "https", "http" }); });
第一个片段在启用 https 的情况下在生产环境中运行,但在默认配置 Visual Studio 中调试时 id 不工作。