0

I am just starting getting into Angular 7. I am trying to place a call to an API that resides on another domain. For that I have added all the needed headers to the API's response. Seems like I am not getting CORS error anymore. But I also dont get into results of my call. Here is some relative code: API:

    getUser() {
        return this.http.get<User[]>('http://localhost:98/user/settings/getuser');
    }

API call:

    test() {
        this.userService.getUser().pipe(first()).subscribe(users => {
            console.log(users);
        });
    }

I am not getting to this code: console.log(users); The Console shows: ERROR OK

Please help. Thanks

4
  • Did you try after removing first()? Also check the network tab of your browser what it returns? Commented Jun 28, 2019 at 17:46
  • A few things. you don't need .pipe(first()). it's implied with http.get(). Also, you can pass a second callback to subscribe(users => ..., err => ...) to catch any errors. Maybe try that and see what you can log. Lastly I would check the network tab to see if your request is going through. Commented Jun 28, 2019 at 17:47
  • Yes, I checked Network tab. I see that there are two requests 'getuser' have been placed. Both return the same: An error occurred attempting to identify user accessing this site. Make sure Windows Authentication is enabled for the site and all other authentication types are disabled. Commented Jun 28, 2019 at 17:51
  • Okay. sounds like it's not Angular issues then, but rather something with your headers or server config. not a windows user so i'm not familiar with this specific error. Commented Jun 28, 2019 at 17:55

1 Answer 1

2

Actually, you only need to subscribe to the method in order to get a response. There's no need to pipe it.

test() {
    this.userService.getUser().subscribe(
    (response) => {
        console.log(response);
    }, 
    (error) => {
        console.log(error);
   });
}

This way you'll be able to see whether there's a CORS error or not in the console. Also, it's usually good to have an error handler in RxJS subscriptions, you might want to catch some errors when your codebase gets bigger.

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.