【发布时间】:2017-05-19 13:21:26
【问题描述】:
我正在尝试从我的 reactjs 客户端(在 webpack 服务器上运行)向我的 java 测试后端发出请求。
两者都是本地主机,但在不同的端口(3000 和 8888)上。
当我使用邮递员向我的后端请求时,我得到了预期的响应,所以一切似乎都很顺利。
但是,当我想从 react 请求时,我遇到了与 CORS 相关的错误:
XMLHttpRequest cannot load http://localhost:8888/api. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:3000' is therefore not allowed access.
我认为我的后端服务器不允许来自我的反应服务器的 http 请求。 所以我需要找到一种方法让我的后端知道处理来自 react 的请求是可以的。
这是我的测试后端。就像我之前所说的,我的后端用作 API 而不是 servlet。
public class test {
public Name getName() {
Name name = new Name("John");
return name;
}
}
有什么办法吗?
【问题讨论】:
-
您需要在响应标头中添加“Access-Control-Allow-Origin”