Angular is not using the api server -


i started angular 4 & spring boot web application. i'm trying call rest path:

ngoninit(): void {   // make http request:   this.http.get('/greeting').subscribe(data => {     // read result field json response.     this.results = data['results'];   }); } 

but resulting url get http://localhost:4200/greeting 404 (not found). i've researched topic , lot of advice add proxy file npm changes routes.

"start": "ng serve --proxy-config proxy.conf.json", 

and proxy.conf.json file:

{     "/api": {         "target": "http://localhost:8080",         "secure": false     } } 

when run npm run start following message:

movie-seat@0.0.0 start
c:\users\alucardu\documents\projects\movieseat\frontend\src\main\frontend> ng serve --proxy-config proxy.conf.json

live development server listening on localhost:4200, open browser on http://localhost:4200/ **

so proxy file loaded has no effect. using outdated information or doing wrong?

i stumbled on answer > redirect angular2 http requests through proxy. , adding line:

"pathrewrite": {"^/api" : ""} 

to proxy.conf.json file fixed problem.

{     "/": {         "target": "http://localhost:8090",         "secure": false,         "pathrewrite": {"^/api" : ""}     } } 

i can call function so:

getgreeting() {   this.http.get('/greeting').subscribe(data => {     // read result field json response.     this.results = data['results'];   }); } 

and calls http://localhost:4200/greeting success , returns correct headers. seems "fix" cors problem. don't need specify cors code in spring-boot backend.


Comments

Popular posts from this blog

angular - Ionic slides - dynamically add slides before and after -

minify - Minimizing css files -

Add a dynamic header in angular 2 http provider -