Reactjs 带反作用力的弹簧法
嗨,我正在尝试用React Front和Spring Java后端创建一个小项目。 假设在Spring控制器类中,我有一个方法:Reactjs 带反作用力的弹簧法,reactjs,spring,fetch,Reactjs,Spring,Fetch,嗨,我正在尝试用React Front和Spring Java后端创建一个小项目。 假设在Spring控制器类中,我有一个方法: @CrossOrigin(origins = "http://localhost:3000/") @GetMapping("/something") public void printSomething() { System.out.println("examplePhrase"); } 在
@CrossOrigin(origins = "http://localhost:3000/")
@GetMapping("/something")
public void printSomething()
{
System.out.println("examplePhrase");
}
在React中,我有一个带有按钮的表单,调用方法“handleSpring”,如下所示:
handleSpring = e =>
{
e.preventDefault();
console.log("Method started");
const SpringAPI = `http://localhost:8080/something`
fetch(SpringAPI, {})
.then(response => console.log("method works"))
.catch(err => console.log("method failed"))
}
当我运行这两个应用程序并转到“localhost:8080/something”时,我在Intellij控制台的“examplephase”中有一条消息,所以Spring方法可以工作。当我要访问“localhost:3000”时,我的表单由React生成。但当我右键点击该站点时,在浏览器菜单中选择“检查”->“控制台”,然后按下表单按钮,我有一个错误:
方法启动
localhost/:1访问以获取'http://localhost:8080/something“起源”http://localhost:3000'已被CORS策略阻止:对飞行前请求的响应未通过访问控制检查:请求的资源上不存在'access control Allow Origin'标头。如果不透明响应满足您的需要,请将请求的模式设置为“no cors”,以获取禁用cors的资源。
App.js:50获取http://localhost:8080/something net::ERR_失败
App.js:50方法失败
我试图添加“no cors”来获取,但它没有改变任何东西。有人能告诉我如何解决这个问题吗?cors origin中的尾部斜杠导致了错误
@CrossOrigin(origins = "http://localhost:3000") // no slash at end in origin.
希望这能解决你的问题。也许我不该承认,但我整个晚上都在为此而挣扎。非常感谢D