Ionic v4 Http get

#1

Hi there,

I´m using inoic 4 and want to make a simple http get but I alwas get Access to XMLHttpRequest at ‘MY_API_LINK’ from origin ‘https://192.168.178.21:8100’ has been blocked by CORS policy: Response to preflight request doesn’t pass access control check: No ‘Access-Control-Allow-Origin’ header is present on the requested resource.

I´m using a Asp.Net Api with basic authentication. I also tried a Chrome plugin to disable cors.

I hope that someone can help me cause I´m really frustrated

Greetings Felix

#2

This one works for me in Chrome…

Also, see this, maybe you can configure your development environment to allow CORS…

#3

Hi IonicGeoff,

thanks for your reply. I solved the problem by using the agnular HttpClient with the request method instead of httpClient.get. I´m not sure why it works now but thanks for your help.

Greetings Felix

#4

Thanks for letting us know. I actually had a problem trying to use http.patch recently, I wonder if I can use your solution? I’ll give it a try.

#5

No problem :slight_smile: It could be also a solution for you. Do you maybe know how i can return the data after I made a subscribe ?
If I try to return this.user after the subscribe the user will be undefined.

this.apiService.getUserByUsername(this.username).subscribe(response => {
            this.user = response
        });
return this.user; 
#6

Well, if you do a console.log(response) you’ll see the structure of the returned data and can apply the values to user.

Not sure why you have return this.user in there?

If you do need return this.user, it MUST be within the subscribe section of your code…

this.apiService.getUserByUsername(this.username)
.subscribe(response => {
            this.user = response;
            return this.user;  // only return the value after it is returned by the service
        });