为什么“服务器没有响应"?在 Swagger UI 中? [英] Why is there "no response from server" in Swagger UI?
问题描述
我的 API 调用在 Postman 中正常工作.但是当我从 Swagger UI 发送请求时,它对所有请求都显示服务器无响应":
My API calls work correctly in Postman. But when I send requests from Swagger UI, it shows "no response from server" for all requests:
响应正文
没有内容
响应代码
0
响应头
{
"error": "no response from server"
}
问题是什么以及如何解决?
What can the problem be and how to fix it?
浏览器控制台显示以下错误:
The browser console shows these errors:
无法加载资源:net::ERR_CONNECTION_REFUSED
Failed to load resource: net::ERR_CONNECTION_REFUSED
未捕获的类型错误:无法读取未定义的属性长度"
在 showStatus (index.js:24)
在 showErrorStatus (index.js:24)
错误 (index.js:607) 在 spec-converter.js:533
在 Request.callback (index.js:24)
在 Request.crossDomainError (index.js:24)
在 XMLHttpRequest.xhr.onreadystatechange (index.js:24)
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: *
响应中的标头.您如何执行此操作取决于您使用的服务器.更多信息在这里:
net::ERR_CONNECTION_REFUSED
sounds like you need to enable CORS on your localhost, so that it sends the Access-Control-Allow-Origin: *
header in responses. How you do this depends on the server you use. More info here:
https://enable-cors.org/server.html
https://github.com/swagger-api/swagger-ui/#cors-support
您可能还需要允许 OPTIONS 飞行前请求.
这篇关于为什么“服务器没有响应"?在 Swagger UI 中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!