简体   繁体   English

将 Angular 连接到 Spring 的 CORS 错误(localhost 8080 <--> localhost 4200)

[英]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 用 Ja​​va 编写的后端。

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 用 Ja​​va 编写的后端。

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.

相关问题 带 Angular 的弹簧靴。 CORS 策略已阻止从源“http://localhost:4200”访问“http://localhost:8080/”处的 XMLHttpRequest - Spring boot with Angular. Access to XMLHttpRequest at 'http://localhost:8080/' from origin 'http://localhost:4200' has been blocked by CORS policy Angular 6 + Spring Boot:错误:“来自源‘http://localhost:4200’已被 CORS 策略阻止” - Angular 6 + Spring Boot: Error: "from origin 'http://localhost:4200' has been blocked by CORS policy" CORS 策略已阻止从 localhost:4200 在 localhost:8080 访问 XMLHttpRequest - Access to XMLHttpRequest at localhost:8080 from localhost:4200 has been blocked by CORS policy 连接到本地主机上的SLiM服务器时出错:8080 - Error connecting to SLiM server on localhost:8080 尝试将请求 /api/v1/mega/building 从 localhost:4200 代理到 http://localhost:8080 时发生错误(ECONNREFUSED) - Error occurred while trying to proxy request /api/v1/mega/building from localhost:4200 to http://localhost:8080 (ECONNREFUSED) 如何在本地主机上的 Spring 和 Angular 2 之间启用 CORS - How to enable CORS between Spring and Angular 2 on localhost 春季靴子| 本地主机:8080 404错误页面显示 - Spring Boot | localhost: 8080 404 error page displayed Tomcat7(localhost:8080):404错误 - Tomcat7 (localhost:8080): 404 error 连接到本地主机时出现连接错误 - Connect to localhost giving error in connecting localhost 4848,8080问题 - localhost 4848, 8080 problem
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM