[英]CORS error connecting Angular to Spring (localhost 8080 <--> localhost 4200)
I am trying to connect my Frontend in Angular 8 to my backend written in Java using Spring.我正在尝试将 Angular 8 中的前端连接到使用 Spring 用 Java 编写的后端。
My Spring backend is running on Tomcat server at localhost:8080 and Angular is open on localhost:4200.我的 Spring 后端在 localhost:8080 的 Tomcat 服务器上运行,Angular 在 localhost:4200 上打开。 When I try to register a user (or do anything), I get this error:当我尝试注册用户(或做任何事情)时,我收到此错误:
Access to XMLHttpRequest at ' http://localhost:8080/Revvit/users ' from origin ' http://localhost:4200 ' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource.从源“ http://localhost:4200 ”访问“ http://localhost:8080/Revvit/users ”的 XMLHttpRequest 已被 CORS 策略阻止:对预检请求的响应未通过访问控制检查:无“访问” -Control-Allow-Origin' 标头存在于请求的资源上。
In my UserController class within Spring, I've annotated the controller with在 Spring 的 UserController 类中,我用
@CrossOrigin(origins= "*", allowedHeaders="*")
@Controller
public class UserController {............
However, I still get the same message in my console as noted above.但是,如上所述,我仍然在控制台中收到相同的消息。
So, I'm assuming that I have to add some header allowing CORS within my Angular project, but WHERE, and HOW would I incorporate this?所以,我假设我必须在我的Angular项目中添加一些允许 CORS 的标题,但是在哪里,以及如何合并它?
I am trying to connect my Frontend in Angular 8 to my backend written in Java using Spring.我正在尝试将 Angular 8 中的前端连接到使用 Spring 用 Java 编写的后端。
My Spring backend is running on Tomcat server at localhost:8080 and Angular is open on localhost:4200.我的 Spring 后端在 localhost:8080 的 Tomcat 服务器上运行,Angular 在 localhost:4200 上打开。 When I try to register a user (or do anything), I get this error:当我尝试注册用户(或执行任何操作)时,出现此错误:
Access to XMLHttpRequest at ' http://localhost:8080/Revvit/users ' from origin ' http://localhost:4200 ' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. CORS 策略阻止了在 ' http://localhost:8080/Revvit/users ' 的 XMLHttpRequest 来自源' http://localhost:4200 ' 的访问:对预检请求的响应未通过访问控制检查:否-Control-Allow-Origin' 标头存在于请求的资源上。
In my UserController class within Spring, I've annotated the controller with在 Spring 中的 UserController 类中,我用
@CrossOrigin(origins= "*", allowedHeaders="*")
@Controller
public class UserController {............
However, I still get the same message in my console as noted above.但是,我仍然在控制台中收到与上述相同的消息。
So, I'm assuming that I have to add some header allowing CORS within my Angular project, but WHERE, and HOW would I incorporate this?所以,我假设我必须在我的Angular项目中添加一些允许 CORS 的标头,但是我将如何合并它?
I've annotated my Controller with我已经注释了我的控制器
@CrossOrigin(origins = "http://localhost:4200")
and it works.它有效。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.