-1

I build a angular 4 application and back with REST API. I have send http get request to get json data using API, but I faced following error:

Failed to load http://localhost:50979/api/workflow: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:4200' is therefore not allowed access.

When I added headers as per the following code :

let headers = new Headers({ 'Content-Type': 'application/json',crossDomain: true});
let options = new RequestOptions({ headers: headers });
return this.http.get(this.url, options)
           .subscribe(
            res =>this.extractData,
            err => this.handleErrorObservable
          );

I have got following error :

url : string = "http://rpa.commandcenter.com/api/workflow"; let data = JSON.stringify(this.EditForm.value);

let headers = new Headers({ 'Content-Type': 'application/json',crossDomain: true});
let options = new RequestOptions({ headers: headers });
return this.http.get(this.url, options)
           .subscribe(
            res =>this.extractData,
            err => this.handleErrorObservable
          );

I can't recognize the problem.

  • 1
    There's nothing you can do in your angular code to solve this problem. The **server** needs to enable CORS requests by adding the required headers to the response, and by accepting preflight options request. Google for "CORS", and read the explanations. Or send requests to the host and post where your application is hosted, and you won't need CORS. – JB Nizet Jan 11 '18 at 10:02
  • "When I added headers as per the following code" — Those headers make **no** sense at all. You are making a GET request. There is no content to describe the type of. `crossDomain` isn't even an HTTP header. It's a **jQuery** setting to tell jQuery not to add optional, custom headers to a same origin request as it might be redirected to a cross origin request and thus generate a preflight request. – Quentin Jan 11 '18 at 10:03
  • "I have got following error :" — That doesn't look like an error message. It looks like source code. Did you copy/paste the wrong thing. – Quentin Jan 11 '18 at 10:04

2 Answers2

0

This is a back-end problem not from your angular code the backend server in your case localhost needs to enable CORS or set up allow origin headers if it can be done from .htacess or from server config file of nginix / apache whatever is good enough.

If this is for development purpose get a browser plugin that allows CORS.

jision
  • 181
  • 2
  • 12
-1

this issue related to backend team , they must allow your domain to access the services, resources

Ahmed Mostafa
  • 666
  • 1
  • 4
  • 16