Angular客户端由于CORS错误无法解析来自Spring Boot的JSON。

huangapple go评论84阅读模式
英文:

Angluar client cant resolve json from spring boot because of cors error

问题

我想创建一个Angular客户端,从本地的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 getProductsBySubcategorieAndCategorie(subcategorie: string, categorie: string): Observable<any> {
    return this.http.get(`${this.baseUrl}/products/subcategorie/${subcategorie}/categorie/${categorie}`);
  }
}

我的问题是我遇到了以下跨域错误。

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应用程序或者我的Spring Boot应用程序中做哪些更改?

英文:

I want to create an angular client, which querys some json from a localhost spring boot server.
my HTTP Angluar service looks so:

export class ProductService {
  private baseUrl = &#39;http://localhost:8080/priceoffer24/api/product&#39;;

  constructor(private http: HttpClient) { }

  public getProductById(id: number): Observable&lt;any&gt; {
    return this.http.get(`${this.baseUrl}/products/${id}`);
  }

  public getProductByName(name: string): Observable&lt;any&gt; {
    return this.http.get(`${this.baseUrl}/products/${name}`);
  }
  public getProductByBrand(brand: string): Observable&lt;any&gt; {
    return this.http.get(`${this.baseUrl}/products/brand/${brand}`);
  }

  public getProductsList(): Observable&lt;any&gt;  {
    return this.http.get(`${this.baseUrl}/products`);
  }

  public getProductsNewest(): Observable&lt;any&gt; {
    return this.http.get(`${this.baseUrl}/products/newest`);
  }

  public getProductsRandom(): any {
    return this.http.get(`${this.baseUrl}/products/random`);
  }

  public getProductsBySubcategorie(subcategorie: string): Observable&lt;any&gt; {
    return this.http.get(`${this.baseUrl}/products/subcategorie/${subcategorie}`);
  }

  public getProductsBySubcategorieId(id: number): Observable&lt;any&gt; {
    return this.http.get(`${this.baseUrl}/products/subcategorie/${id}`);
  }

  public getProductsByCategorie(categorie: string): Observable&lt;any&gt; {
    return this.http.get(`${this.baseUrl}/products/categorie/${categorie}`);
  }

  public getProductsByCategorieId(id: number): Observable&lt;any&gt; {
    return this.http.get(`${this.baseUrl}/products/categorie/${id}`);
  }

  public getProductsBySubcategorieAndCategorie(subcategorie: string, categorie: string): Observable&lt;any&gt; {
    return this.http.get(`${this.baseUrl}/products/subcategorie/${subcategorie}/categorie/${categorie}`);
  }

}

My problem is now that I get the following cors error.

Access to XMLHttpRequest at &#39;http://localhost:8080/priceoffer24/api/product/products/newest&#39; from origin &#39;http://localhost&#39; has been blocked by CORS policy: No &#39;Access-Control-Allow-Origin&#39; header is present on the requested resource.

What do I have to change in my angular app, or in my spiring boot app ?

答案1

得分: 1

问题不在Angular应用程序中,你需要将客户端应用程序的URL(默认应为http://localhost:4200)添加到Api服务器(Spring Boot)中的允许源列表中。

因此,要解决这个问题,只需搜索如何修复服务器端的问题。
可能这个链接有帮助:https://spring.io/guides/gs/rest-service-cors/

英文:

The issue isn't in the Angular App, you have to add your client app URL (default should be http://localhost:4200) to the Allowed Origins in the Api Server (spring boot).

So to resolve this just search how to fix the server side.
Probably this can help https://spring.io/guides/gs/rest-service-cors/

答案2

得分: 0

使用@CrossOrigin(origins = "http://example.com")来覆盖你的Spring Boot REST控制器。

这里的**http://example.com**指的是Angular应用的URL(客户端应用)。这样做将只允许特定的应用访问你的应用。

如果你想允许所有主机访问你的控制器,只需直接使用@CrossOrigin

英文:

Use @CrossOrigin(origins = &quot;http://example.com&quot;) over your spring boot rest controller.

Here http://example.com refers to angular app URL (Client App). Doing this will allow only the particular application to access your application.

If you want to allow all hosts to access your controller then just use @CrossOrigin directly.

huangapple
  • 本文由 发表于 2020年9月18日 03:27:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/63944995.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定