0

I facing issue with fetching data from my laravel server.

This is client side code:

private surveysUrl = 
'http://107.170.59.79/services/public/api/v1/countries';
private headers = new Headers({'Content-Type': 'application/json'});

constructor(private http: Http) { }

getSurveys(): Promise<Survey[]> {
return this.http.get(this.surveysUrl)
   .toPromise()
   .then(response => response.json() as Survey[])
   .catch(this.handleError);
}

But I am receiving as error as:

XMLHttpRequest cannot load {{link set in variable "surveysUrl"}}. The 'Access-Control- Allow-Origin' header has a value '{{Some random link}}' that is not equal to the supplied origin. Origin 'http://localhost:4200' is therefore not allowed access.

How can I fix this error?

PS: I am not allowed to post more than two links so I had to remove links from the error message. I have replaced it with {{ }} to make it readable.

3
  • 2
    Possible duplicate of How to make CORS-enabled HTTP requests in Angular 2? Commented Jun 2, 2017 at 7:33
  • It's only 49.248.126.222:8282 that has access, you need to change that to allow your localhost, or then allow all. This has to be done server side, not Angular as you asked below. Commented Jun 2, 2017 at 8:49
  • 1
    Thanks @AJT_82. I will make the changes. Commented Jun 2, 2017 at 9:26

1 Answer 1

3

If you are using apache then you need to allow the origin access i.e.

Header set Access-Control-Allow-Origin "*"

include this in .htaccess file.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for the suggestion but can you please let me know if I can handle it from Angular.
Hi @RumSmeagolRun this is something to be done at server side. (i.e the one who is creating the API end point for you)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.