1

I am trying to call REST APIs from development environment(localhost:4200) using Angular 2 (version 4.0.0)

getting the below error message: "Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at https://localhost:8000/auth. (Reason: CORS header ‘Access-Control-Allow-Origin’ missing)."

Here I am providing a code sample for reference:

var headers = new Headers();
headers.append('Content-Type', 'application/x-www-form-urlencoded;charset=UTF-8');

return this.http.post(this.loginUrl, 'POST_Parameters', {
       headers: headers
}).map(res => {
       console.log(res);
});

Can someone please help me to find out solution for this issue?

  • 2
    Possible duplicate of [How to create cross-domain request (Angular 2)?](http://stackoverflow.com/questions/34790051/how-to-create-cross-domain-request-angular-2) – AT82 May 03 '17 at 14:56

4 Answers4

1

CORS must be enabled by backend. Depending on backend framework, usually you must send specific header that backend can recognize. But until you explicitly enable CORS support on backend, sending header is useless

A. Tim
  • 3,046
  • 1
  • 13
  • 15
1

on a project of mine i just went to my server (apache2 on linux 16.04 ) and i just added the "Header set Access-Control-Allow-Origin "*"" on the virtual server config file

ircmgr
  • 77
  • 1
  • 9
0

As per Browser policy CORS is not allowed. If you are using POSTMAN then this error won't not occur.

CORS error should only remove by server side. Hence you should set header in your backend code.

If you are using express as your backend ...

app.use(function(req,res){
    res.header('Access-Control-Allow-Origin', '*');});
thangavel .R
  • 466
  • 4
  • 13
  • But as per scenario here, I have no control on REST Services, Client provided us services that need to call. that's all. Even I my self do not know about platform on which services are created. If any work around to get services response will be great help. – vaibhav Srivastava May 04 '17 at 06:08
0

You are in local server. You need to run api and angular app on same server. When you upload project on same production server I hope This error will not shown.

Sourav Golui
  • 583
  • 1
  • 6
  • 13