I am trying to resolve CORS Policy issue with Access-Control-Allow-Origin header using Proxy.conf.json with my Angular application. I don't have much experience with Angular and after trying so many google searches I did below configuration with my application and it's not working for me.
It seems like Angular is not going to proxy, api calls works as if there is nothing specify in proxy.
I am using Node to host my application and I have following command to start my application in package.json
"ng": "ng",
"start": "ng serve --host 0.0.0.0 -p 8080 --proxy-config proxy.conf.json",
my environment.ts file has below reference.
"proxyService":"http://machinename:7171/esbapi/servicedetails/v1/proxyservice"
I am calling above api using post
return this.http.post(environment.proxyService, jsonForPost).map((res: Response) => res.json());
I have following code in my proxy.conf.json. I have also tried with "/esbapi/* but I am still not getting it to work.
{
"/esbapi/servicedetails/v1/proxyservice*" :{
"target":"http://machinename:7171/esbapi/servicedetails/v1/proxyservice",
"secure": false,
"changeOrigin" : true,
"logLevel": "debug"
}
}
I am still getting CORS issue when I use the above config. Proxy seems to be not working.
I have also tried below steps.
In one post it was mentioned that having fixed URL path may not work with post method so I also tried changing value in my
environment.ts
"proxyService":"/esbapi/servicedetails/v1/proxyservice"
and tried to create proxy for that which did not work as well.
Instead of calling
"target":"http://machinename:7171/esbapi/servicedetails/v1/proxyservice",
it was calling
http://localhost:8080/esbapi/servicedetails/v1/proxyservice
The code did not pick up the target provided in proxy conf file.
Please let me know if I am doing something wrong here.
Some More details -------
When I start the server I can see following information in console.
> ng serve --host 0.0.0.0 -p 8080 --proxy-config proxy.conf.json
** NG Live Development Server is listening on 0.0.0.0:8080, open your browser on http://localhost:8080/ **
10% building modules 3/3 modules 0 active[HPM] Proxy created: /esbapi/servicedetails/v1/proxyservice* -> http://machinename:7171
[HPM] Subscribed to http-proxy events: [ 'error', 'close' ]
And when I make a call from UI below line is printed on Console
[HPM] POST /esbapi/servicedetails/v1/proxyservice -> http://machinename:7171/esbapi/servicedetails/v1/proxyservice
I turned on browser console and I can see following message
POST http://localhost:8080/esbapi/servicedetails/v1/proxyservice net::ERR_EMPTY_RESPONSE