1

I'm new on this and I think this question will be easy for u, but please help.

I need to make on loading page http request and it's working but my query is blocked with message

Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at http://localhost:9999/. (Reason: CORS header ‘Access-Control-Allow-Origin’ missing).

There is my code of Angular :

export class AppComponent implements OnInit {

  public apiURL:string = 'http://localhost:9999/';
  public logged: boolean;
  response: any;

  constructor(private elementRef:ElementRef, private http:Http) {
        this.logged = this.elementRef.nativeElement.getAttribute('auth');
  }

  ngOnInit() {
    this.checkLogin()
  }

  checkLogin() {
    this.response = this.http.post(this.apiURL, {"search": "search"}, ).map(res => res.json()).subscribe(data => {console.log(data);});
  }
}

I already tried a many variants.. but no one is not working for me... could someome provide little example of resolving it... please...

olexii.lavrov
  • 109
  • 10
  • If you created application with Angular cli then it is from server side. You need to allow all origin from server side. – mukund patel Dec 19 '17 at 18:25
  • Did you use a nodejs to create your http service provider? Maybe share the server side code if you could. – mohsenmadi Dec 19 '17 at 18:31

1 Answers1

1

This is an issue on the server and not the client. Your browser is preventing you from making a call to a different host as a security measure. If you have access to the server you need to add:

Access-Control-Allow-Origin: *

Otherwise you will need a proxy to call the server

See: Origin is not allowed by Access-Control-Allow-Origin

Eeks33
  • 2,245
  • 1
  • 14
  • 17