Angular应用程序与部署在Docker容器中的SpringBootAPI之间的通信问题。
创始人
2024-10-30 14:30:24
0

如果您正在使用Docker部署Spring Boot API,可以按照以下步骤设置CORS(跨域资源共享)以解决此问题:

  1. 在Spring Boot API的应用程序配置文件application.properties或application.yml中添加以下代码:

application.properties

# CORS configuration
spring.mvc.cors.allowed-origins= http://localhost:4200
spring.mvc.cors.allowed-methods= GET, POST, PUT, DELETE, OPTIONS
spring.mvc.cors.allowed-headers= *

application.yml

# CORS configuration
spring:
  mvc:
    cors:
      allowed-origins: http://localhost:4200
      allowed-methods: GET, POST, PUT, DELETE, OPTIONS
      allowed-headers: "*"

修改allowed-origins以匹配您的Angular应用程序的基本URL。

  1. 在Angular应用程序的服务文件中添加以下HTTP头文件:
import { HttpHeaders } from '@angular/common/http';

const httpOptions = {
  headers: new HttpHeaders({
    'Access-Control-Allow-Origin': 'http://localhost:8080',
    'Access-Control-Allow-Methods': 'GET, POST, PUT, DELETE',
    'Access-Control-Allow-Headers': 'Origin, X-Requested-With, Content-Type, Accept'
  })
};

将Access-Control-Allow-Origin和Access-Control-Allow-Headers的值更改为Spring Boot API运行的端口和请求的协议。

  1. 将httpOptions应用于HTTP请求。
import { HttpClient } from '@angular/common/http';
@Injectable({
  providedIn: 'root'
})
export class ApiService {
  constructor(private http: HttpClient) { }
  getUsers() {
    return this.http.get('http://localhost:8080/api/users', httpOptions);
  }
}

使用上述步骤,您现在应该能够成功地通过Docker容器部署的Spring Boot API与Angular应用程序通信。

相关内容

热门资讯

透视挂!wpk透视脚本,hhp... 透视挂!wpk透视脚本,hhpoker免费辅助器,总结教程(有挂工具)1、wpk透视脚本透视辅助简单...
透视透视"aapok... 透视透视"aapoker真的假的"详细辅助安装教程(辅助透视技巧)1、许多玩家不知道aapoker真...
透视系统!智星德州菠萝插件官网... 透视系统!智星德州菠萝插件官网,wepoker的辅助器,教你攻略(有挂细节)该软件可以轻松地帮助玩家...
透视安装"hhpok... 这是一款非常优秀的hhpoker是正规的吗 ia辅助检测软件,能够让你了解到hhpoker是正规的吗...
透视了解!aapoker辅助挂... 透视了解!aapoker辅助挂,wepoker究竟有没有透视,线上教程(有挂脚本);1、aapoke...
透视有挂"epoke... 透视有挂"epoker底牌透视"详细辅助新版2025教程(辅助软件方法)1、很好的工具软件,可以解锁...
透视挂!hhpoker透视脚本... 透视挂!hhpoker透视脚本视频,wepoker数据分析工具,技巧教程(有挂黑科技)1、让任何用户...
透视神器"德州局怎么... 此外,数据分析德州(德州局怎么透视)辅助神器app还具备辅助透视行为开挂功能,通过对客户德州局怎么透...
透视挂!德普之星辅助功能如何设... 透视挂!德普之星辅助功能如何设置,德普之星辅助器,AI教程(有挂插件);1、下载好德普之星辅助功能如...
透视透视"wepok... 透视透视"wepoker有脚本吗"详细辅助爆料教程(辅助器下载软件)1、很好的工具软件,可以解锁游戏...