0

This is my service

export class user {
    apiUrl = 'http://localhost:8080/api/v1/'

    constructor(private _http: HttpClient) {}

    getUser() {
     return this._http.get(this.apiUrl + `user`, {});
}

how to add a header for CORS control ?

Ali Bigdeli
  • 1,286
  • 3
  • 17
  • 35
Hamza Bouallegue
  • 101
  • 2
  • 13
  • 2
    `CORS` is server side – Jacopo Sciampi Jan 28 '20 at 09:12
  • Your Api has to provide `Access-Control-Allow-Origin`, `Access-Control-Allow-Methods` and `Access-Control-Allow-Headers` headers with the response. – frido Jan 28 '20 at 10:20
  • @fridoo exemple please ? – Hamza Bouallegue Jan 28 '20 at 10:36
  • @HamzaBouallegue You have to search for how to provide these headers with the server you're using. For a Go server it can look like this https://stackoverflow.com/a/55904938/9423231. General information about CORS: https://stackoverflow.com/a/35553666/9423231 – frido Jan 28 '20 at 10:43

1 Answers1

0

I know its too late but for others, the only way to to disable/enable CORS in Angular is use proxy configure as your angular option.

for example you can put proxy.json into assets folder and fill it as below contents:

assets/proxy.json :

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

and change your serve/build with below code in angular.json file :

"architect": {
    "serve": {
            "builder": "@angular-devkit/build-angular:dev-server",
            "options": {
                "browserTarget": "your-project-name:build",
                "proxyConfig": "assets/proxy.json"
            },
          }
}

NOTE: don't forget to enable CORS header in your server side project.

piet.t
  • 11,718
  • 21
  • 43
  • 52
Ali Bigdeli
  • 1,286
  • 3
  • 17
  • 35