Thursday, 24 November 2016

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

Answer



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

c++ - Does curly brackets matter for empty constructor?

Those brackets declare an empty, inline constructor. In that case, with them, the constructor does exist, it merely does nothing more than t...