0

I am new to Angular2. I am trying to implement an authentication to an app with a username and password login credentials but I always get a "No Access-Control-Allow-Origin"

No access allow control origin

below is my code:

  onSubmit() {
    let headers = new Headers();
    headers.set('Content-Type', 'application/json');
    headers.set('Access-Control-Allow-Origin', 'http://local.press.hosting:8080');
    headers.set('Access-Control-Allow-Credentials', 'true');
    headers.set('Access-Control-Allow-Headers', 'X-Requested-With,content-type');

  this.http.post('http://staging.press.hosting:9000/api/v1/adminLogin', JSON.stringify({"username":this.username,"password":this.password}),{headers:headers})
        .subscribe((res)=>{
            console.log(res)
        });
  }
Francesco Borzi
  • 56,083
  • 47
  • 179
  • 252
asHkER
  • 415
  • 4
  • 16
  • 2
    `Access-Control-Allow-Origin` is a response header by your server, not request header. You need to set on server side. See http://stackoverflow.com/questions/10636611/how-does-access-control-allow-origin-header-work – Luke Hutton Jan 20 '17 at 02:26
  • @LukeHutton you should post that as an answer instead of a comment, it's the correct answer –  Jan 20 '17 at 02:27
  • why did you cancel my edit? I changed the AngularJS tag with Angular. Check here: http://angularjs.blogspot.nl/2016/12/ok-let-me-explain-its-going-to-be.html – Francesco Borzi May 20 '17 at 08:39

1 Answers1

1

Access-Control-Allow-Origin is a response header set by your server code. In this case, the api hosted at staging.press.hosting:9000. It is not a request header needed to be set by the client.

You need to set this response header in your api code.

See this detailed answer for more information as it shouldn't be needed to repeat here: https://stackoverflow.com/a/10636765/368552

Community
  • 1
  • 1
Luke Hutton
  • 10,612
  • 6
  • 33
  • 58