Cześć napisałem sobie proste Restowe API w Springu, a teraz w Angularze chciałbym się do niego odwołać. Problem polega na tym, że nie bardzo rozumiem jak podać parametry do zapytania GET.
Więc tak tak wygląda to w Springu
@CrossOrigin(origins = "http://localhost:4200")
@RequestMapping(
value = "/algorithm/fifo",
params = {"virtualMemory", "physicalMemory", "stringReference"})
public Data fifo(
@RequestParam(value = "virtualMemory", defaultValue = "3", required = false) String virtualMemory,
@RequestParam(value = "physicalMemory", defaultValue = "3", required = false) String physicalMemory,
@RequestParam(value = "stringReference", defaultValue = "3", required = false) String stringReference) {
algorithmService.setAlgorithmsData(virtualMemory, physicalMemory, stringReference);
return algorithmService.getData();
}
Teraz w Angularze mam HttpService z taką metodą
getAlgorithmData() {
let params: URLSearchParams = new URLSearchParams();
params.set('virtualMemory', '1');
params.set('physicalMemory', '2');
params.set('stringReference', '3');
return this.http.get('http://localhost:8080/algorithm/fifo', { search: params})
.map((response: Response) => response.json());
}
Wiem, że prawdopodobnie źle tu przekazuje te parametry. Trochę patrzyłem na innym forum jak to zrobić, ale zawsze mam jakiś błąd i nie potrafię go rozwiązać.
W komponencie natomiast tak pobieram dane
getAlgorithmData() {
this.httpService.getAlgorithmData().subscribe(
(data: any) => this.algorithmData = data
);
}
No i błąd jaki dostaję to
GET http://localhost:8080/algorithm/fifo 400 ()
XMLHttpRequest cannot load http://localhost:8080/algorithm/fifo. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:4200' is therefore not allowed access. The response had HTTP status code 400.
ERROR Response {_body: ProgressEvent, status: 0, ok: false, statusText: "", headers: Headers…}
scibi92