Rest API. Metoda get z parametrami.

0

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…}
0

Przecież widać po logu że nie o to chodzi. Zresztą Ty to jakoś dziwnie robisz, serwujesz Angulara odrębnie od REST API?

0

Nie rozumiem pytania. Backend mam Javie front w Angularze. No właśnie nie bardzo rozumiem co się dzieje. Bo dla testu zmieniłem metodę na

    @CrossOrigin(origins = "http://localhost:4200")
    @RequestMapping(value = "/algorithm/fifo")
    public Data fifo() {
        return new Data(1,4,1);
    }

I wtedy dostaję odpowiedź.

0

Możesz pokazać repo?
Chodzi mi o to czy angular i backend są wystawiane jako różne aplikacje webowe ;)

0

Repo nie ma. Bo do tego co chce zrobić nie jest mi potrzebne. Na podstawie parametrów GET tworzę obiekt, który od razu zwracam. Wygląda to tak

http://localhost:8080/algorithm/fifo?virtualMemory=1&physicalMemory=2&stringReference=3
{"virtualMemory":1,"physicalMemory":2,"stringReference":3}

Tak dwie osobne appki. Źle nie tworzę obiektu tylko ustawiam jego wartości.

0

No właśnie zrobiłem podobnie jak na stronie springa. Nie rozumiem czemu mi ten cors... wywala.

0

Musisz dodać import "URLSearchParams" w "http.service.ts":

import {Http, Response, URLSearchParams} from '@angular/http';

1 użytkowników online, w tym zalogowanych: 0, gości: 1