我正试图在后端调用一个api,但我总是收到一个连接被拒绝的错误。后端位于部署在bluehost中的tomcat服务器中;框架为Spring Boot;其war文件已部署在webapp文件夹中;我已经在web.xml中包含了以下过滤器,以允许cors
<filter>
<filter-name>CorsFilter</filter-name>
<filter-class>org.apache.catalina.filters.CorsFilter</filter-class>
<init-param>
<param-name>cors.allowed.orgins</param-name>
<param-value>*</param-value>
</init-param>
<init-param>
<param-name>cors.allowed.methods</param-name>
<param-value>GET,POST,HEAD,OPTIONS,PUT</param-value>
</init-param>
<init-param>
<param-name>cors.allowed.headers</param-name>
<param-value>Content-Type,X-Requested-With,accept,Origin,Access-Control-Request-Method,Access-Control-Request-Headers</param-value>
</init-param>
<init-param>
<param-name>cors.exposed.headers</param-name>
<param-value>Access-Control-Allow-Origin,Access-Control-Allow-Credentials</param-value>
</init-param>
<init-param>
<param-name>cors.support.credentials</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>CorsFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
因为我相信cors在springboot中已经默认启用了,所以我没有费心去定义cors配置bean文件;即使我做了,我仍然收到错误。我还确保我的客户端请求没有包含任何非法的头。但是,它正在使用内容类型执行飞行前请求。具体如下:
let httpReq = {
method: type,
headers: {
"Content-Type": "application/json",
Accept: "application/json"
},
referrer: window.location.href, // https://web.dev/referrer-best-practices/
referrerPolicy: "strict-origin-when-cross-origin",
mode: "cors",
credentials: "same-origin",
cache: "default",
redirect: "error"
};
await fetch("http://localhost:8080" + api, httpReq)
.then(response => {
return response.json();
})
我重新启动了apache和tomcat好几次,仍然一无所获。我也看不到来自tomcat的任何有用的日志,而且我还确保了这个过程仍然与netstat一起工作。我不知道该怎么办。如有任何建议,将不胜感激。
谢谢
暂无答案!
目前还没有任何答案,快来回答吧!