Vaibhav Singh
Vaibhav Singh

Reputation: 71

Http request working on ionic serve but doesn't work on "ionic cordova run android --device"

I am using Ionic native http to make server requests. The server is hosted on aws and the apis are up and running (verified by postman).

When i do ionic serve the requests go through and work but when i try to run it on device it doesnt work. On inspection i get "net::ERR_CLEARTEXT_NOT_PERMITTED" and post is sent as OPTIONS

ERROR HttpErrorResponse {headers: HttpHeaders, status: 0, statusText: "Unknown Error", url: "http://18.220.143.133/api/login", ok: false, …}

already tried adding to config.xml

login(user: User): Observable<AuthResponse> {
return this.httpClient.post('http://18.220.143.133/api/login', user, {
  headers: new HttpHeaders().set('Content-Type', 'application/json'),
}).pipe(
  tap(async (res: AuthResponse) => {

    if (res.success == true) {

      await this.storage.set('user',res.user);

       await this.storage.set('ACCESS_TOKEN', res.token);
      this.authSubject.next(true);
    }

  })
);

}

I expect http requests to go through from mobile devices as well.

Upvotes: 0

Views: 1368

Answers (2)

Juan Diego Garcia
Juan Diego Garcia

Reputation: 201

The same happened to me, until in the config.xml file I added:

<preference name = "android-targetSdkVersion" value = "27" />

Upvotes: 1

Stephen Romero
Stephen Romero

Reputation: 3032

It has to do with your CORS for the API call. This may help. Also, since you're using Angular HTTP requests, check here HTTP request from angular will send with method OPTIONS instead of POST.

Upvotes: 0

Related Questions