Monday 11 June 2018

typescript - Enable Access-Control-Allow-Origin in angular 5 from Client side

I am using httpClient angular package for get requests. I am using https://api.independentreserve.com/Public/GetValidPrimaryCurrencyCodes url for fetching data. It is giving CORS error in console.



I don't have access to server side code, but still I want to enalbe CORS in angular service. This is my service function



myfunction() {
let head = new HttpHeaders();
head.append('Access-Control-Allow-Headers', 'Content-Type');
head.append('Access-Control-Allow-Methods', 'GET');

head.append('Access-Control-Allow-Origin', '*');
return this.http.get("https://api.independentreserve.com/Public/GetValidPrimaryCurrencyCodes", {headers: head}).toPromise();
}


I have tried it with chrome cors extension, and it works fine. But I want this to be enabled from angular code.



I have also tried working with proxies, following tutorial. BUt it is still not working. This is my object in proxy.conf.json file.



{

"/api": {
"target": "https://www.independentreserve.com/Public/GetValidPrimaryCurrencyCodes",
"secure": false
},
"changeOrigin": true
}


and added "start": "ng serve --proxy-config proxy.conf.json" in package.json file. It is not working this way either.




Can any body give me some solution?

No comments:

Post a Comment

php - file_get_contents shows unexpected output while reading a file

I want to output an inline jpg image as a base64 encoded string, however when I do this : $contents = file_get_contents($filename); print &q...