0

I am calling my get rest service like this,

  makeGetCall(url: string): Observable<any> {
    const headers = new Headers({'auth-code': 'auth-code',
                                  'from':'app'});
    return this.http.get(AdminConstants.BASE_URL + this.SEPARATOR + url,{
      headers: headers
    })
      .map(this.extractData)
      .catch(this.handleErrorObservable);
  }
  private extractData(res: Response) {
    console.log('extract data');
    console.log(res);
    const body = res.json();
    return body || {};
  }

  private handleErrorObservable(error: Response | any) {
    console.log('error in service');
    console.log(error.message);
    return Observable.throw(error.message || error);
  }

and My rest service is:

console.log('inside get all user');
        ApiUser.find().then(users => {
            console.log('length : '+users.length)
            res.status(200).send(users);
        }, error => {
            res.send(500).send(error);
        })

I am using MEAN stack for my application. Here is my problem, when I am calling my rest service from postman its working.Even in the chrome/mozilla console I can see my response as expected.response headerresponseboby

But from my angular 2 application my handleErrorObservable method is called and that too with an error without proper message.

I just installed ssl certificates on my server, before ssl this was working inside my application. Also to add post requests are working.

I don't know what I am missing. Please help. Thanks in advance.

BhaskerYadav
  • 569
  • 3
  • 24
  • 1
    what's the error? – d.moncada Sep 24 '17 at 18:00
  • This might be the issue of http preflight OPTIONS call. Check this [SO answer](https://stackoverflow.com/a/46123053/2015408) – Surender Khairwa Sep 24 '17 at 18:01
  • This isn't a preflight request as data is being returned, but you are missing `Acess-Control-Allow-Origin` in the response headers, so it looks like a general CORS issue. – Kirk Larkin Sep 24 '17 at 18:04
  • @SurenderKherwa will search on these lines – BhaskerYadav Sep 24 '17 at 18:05
  • @KirkLarkin : in this I should not get the response also, right ? – BhaskerYadav Sep 24 '17 at 18:06
  • 1
    Everything is returned as normal. It's actually the browser that effectively creates the error when CORS is not satisfied. You would need your response headers to include `Access-Control-Allow-Origin: *` or whatever your domain is instead of `*`. Treat this as a CORS issue and do some research. Also check the console in Chrome - You'll likely see an error in there. – Kirk Larkin Sep 24 '17 at 18:09
  • @KirkLarkin thank you. Will search on this – BhaskerYadav Sep 24 '17 at 18:10
  • 1
    @KirkLarkin : you were right. as soon as I added the response in my rest service it worked. – BhaskerYadav Sep 24 '17 at 18:27

1 Answers1

0

Thanks to @kirkLarkin

I just added res.set('Access-Control-Allow-Origin','*'); in my rest service and it worked.

BhaskerYadav
  • 569
  • 3
  • 24