【发布时间】:2016-12-05 07:54:21
【问题描述】:
我目前正在学习使用 React,但我遇到了一个问题:我想通过我的 Java Spring Boot 后端 (Tomcat) 使用 react。 React 在端口 3000(默认)上运行,我的 Tomcat 服务器在端口 8080(默认)上运行。现在,当我进行 REST 调用时,出现以下错误
script.js:13 GET http://localhost:8080/api/path?p=test net::ERR_CONNECTION_REFUSED
我的休息电话看起来像:
fetch('http://localhost:8080/api/path?p=test')
.then(res => {
console.log(res);
});
我做错了什么?我真的不知道。
【问题讨论】:
-
您是否尝试使用邮递员或可能发布的应用程序发送请求?其他情况,您的后端是否接受跨源请求?
-
在您的浏览器中尝试localhost:8080/api/path?p=test。有用吗?
-
连接被拒绝的意思正是它所说的,由于某种原因它无法与您的后端通信。检查您的路径是否正确,如果您的后端正在运行,... 。我们目前的信息无法解决这个问题。
-
好的,谢谢@g00glen00b .. 我很愚蠢,我的后端没有运行。现在我的连接被接受了,但我得到了
No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:3000' is therefore not allowed access. If an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled. -
@ChristianPeters spring.io/guides/gs/rest-service-cors
标签: rest reactjs spring-boot