在Angular的service中,我们需要将baseURL设置为localhost:8080而不是默认的localhost:4200。我们可以通过在service中使用HttpClientModule来实现这一点。以下是示例代码:
import { Injectable } from '@angular/core'; import { HttpClient } from '@angular/common/http'; import { Observable } from 'rxjs';
@Injectable({ providedIn: 'root' }) export class ApiService { private BASE_URL = 'http://localhost:8080';
constructor(private http: HttpClient) {}
public getItems(): Observable${this.BASE_URL}/api/items
);
}
}
在Spring Boot的后端代码中,我们需要允许来自localhost:4200的请求。我们可以通过在后端代码中添加以下代码来实现这一点:
@RestController @CrossOrigin(origins = "http://localhost:4200") public class ItemController { // REST API methods }
以上是示例代码,我们需要将 origins 参数设置为我们希望允许的来源。在这种情况下,我们设置为localhost:4200。
这些更改将确保Angular Service在请求时使用正确的URL。