【发布时间】:2021-01-04 18:11:22
【问题描述】:
我想创建一个 Angular 客户端,它从 localhost spring boot 服务器查询一些 json。 我的 HTTP Angular 服务看起来是这样的:
export class ProductService {
private baseUrl = 'http://localhost:8080/priceoffer24/api/product';
constructor(private http: HttpClient) { }
public getProductById(id: number): Observable<any> {
return this.http.get(`${this.baseUrl}/products/${id}`);
}
public getProductByName(name: string): Observable<any> {
return this.http.get(`${this.baseUrl}/products/${name}`);
}
public getProductByBrand(brand: string): Observable<any> {
return this.http.get(`${this.baseUrl}/products/brand/${brand}`);
}
public getProductsList(): Observable<any> {
return this.http.get(`${this.baseUrl}/products`);
}
public getProductsNewest(): Observable<any> {
return this.http.get(`${this.baseUrl}/products/newest`);
}
public getProductsRandom(): any {
return this.http.get(`${this.baseUrl}/products/random`);
}
public getProductsBySubcategorie(subcategorie: string): Observable<any> {
return this.http.get(`${this.baseUrl}/products/subcategorie/${subcategorie}`);
}
public getProductsBySubcategorieId(id: number): Observable<any> {
return this.http.get(`${this.baseUrl}/products/subcategorie/${id}`);
}
public getProductsByCategorie(categorie: string): Observable<any> {
return this.http.get(`${this.baseUrl}/products/categorie/${categorie}`);
}
public getProductsByCategorieId(id: number): Observable<any> {
return this.http.get(`${this.baseUrl}/products/categorie/${id}`);
}
public getProductsBySubcategorieAndCategorie(subcategorie: string, categorie: string): Observable<any> {
return this.http.get(`${this.baseUrl}/products/subcategorie/${subcategorie}/categorie/${categorie}`);
}
}
我现在的问题是出现以下 cors 错误。
Access to XMLHttpRequest at 'http://localhost:8080/priceoffer24/api/product/products/newest' from origin 'http://localhost' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.
我必须在我的 Angular 应用程序或我的 spiring boot 应用程序中进行哪些更改?
【问题讨论】:
-
您需要在 Spring Boot 应用中启用 CORS。
标签: java angular spring-boot http cors