title: Ionic angular http post not working on device but works fine on browser
body: <p>I'm working on an ionic-angular app and post request is working fine when tested on browser using ionic serve but when excecuted on anroid device or emulator, the request doesn't work.</p>
  
  <p>The backend is a rest api using slim php and xampp on windows server</p>
  
  <p>The response for the request in android device is:</p>
  
  <blockquote>
    <p>{"headers":{"normalizedNames":{},"lazyUpdate":null,"headers":{}},"status":0,"statusText":"Unknown Error","url":"http://&lt;>/ws_login.php/login","ok":false,"name":"HttpErrorResponse","message":"Http failure response for http://&lt;>/ws_login.php/login: 0 Unknown Error","error":{"isTrusted":true}}</p>
  </blockquote>
  
  <p>My login code looks like:</p>
  
  <pre><code>import { Component, OnInit } from '@angular/core';
  import { Router } from '@angular/router';
  import { HttpClientModule, HttpClient, HttpHeaders } from '@angular/common/http';
  import { AuthConstants } from '../config/auth-constants';
  import { ToastService } from './../services/toast.service';
  import { LoadingService } from './../services/loading.service';
  import { StorageService } from './../services/storage.service';
  import { environment } from '../../environments/environment';
  
  @Component({
      selector: 'app-login',
      templateUrl: './login.page.html',
      styleUrls: ['./login.page.scss'],
  })
  export class LoginPage implements OnInit {
      data = {
          user : '',
          pass : ''
      }
  
      constructor(
          public http: HttpClient, 
          private router: Router, 
          private toastService: ToastService,
          private loadingService: LoadingService,
          private storageService: StorageService
      ) { }
  
      ngOnInit() {
          this.storageService.get(AuthConstants.AUTH).then(
              data =&gt; data?this.router.navigate(["home"]):null
          )
      }
  
      login() {
          var headers = new HttpHeaders();
          headers.append("Accept", 'application/json');
          headers.append('Content-Type', 'application/json' );
          const requestOptions = { headers: headers };
  
          if(this.data.user == "" || this.data.pass == "")
              return this.toastService.presentToast("Debe completar ambos campos", "danger");
  
          this.loadingService.showLoading("Iniciando sesión");
          console.log(environment.apiUrl+'ws_login.php/login');
          this.http.post(environment.apiUrl+'ws_login.php/login', this.data, requestOptions).subscribe(
              resp =&gt; {
                  this.loadingService.hideLoading();
                  if(resp){
                      if(resp['error'] == 1)
                          this.toastService.presentToast(resp['mensaje'], "danger");
                      else{
                          this.storageService.store(AuthConstants.AUTH, 1);
                          this.toastService.presentToast("Sesión iniciada con éxito", "success");
                          this.router.navigate(["home"]);
                      }
                  }
                  else
                      this.toastService.presentToast("Error de red", "danger");
              }, 
              error =&gt; {
                  this.data.user = JSON.stringify(error);
                  this.loadingService.hideLoading();
                  this.toastService.presentToast("Error de red", "danger");
              }
          );/*
          console.log(AuthConstants.AUTH);
          AuthConstants.AUTH = {id: 1};*/
          //this.router.navigate(["home"]);
      }
  }
  </code></pre>
  
  <p>I also test the api using a rest app on device and the same url answers without problem.</p>
  
  <p>I added android:usesCleartextTraffic="true" to androd manifest and was not the solution.</p>
  
  <p>Here mi ionic info</p>
  
  <pre><code>Ionic:
  
     Ionic CLI                     : 6.3.0 (C:\Users\Manu\AppData\Roaming\npm\node_modules\@ionic\cli)
     Ionic Framework               : @ionic/angular 5.0.7
     @angular-devkit/build-angular : 0.803.26
     @angular-devkit/schematics    : 8.3.26
     @angular/cli                  : 8.3.26
     @ionic/angular-toolkit        : 2.2.0
  
  Capacitor:
  
     Capacitor CLI   : 2.0.0
     @capacitor/core : 2.0.0
  
  Utility:
  
     cordova-res : 0.11.0
     native-run  : 0.3.0
  
  System:
  
     NodeJS : v12.16.1 (C:\Program Files\nodejs\node.exe)
     npm    : 6.13.4
     OS     : Windows 10
  </code></pre>
  
  <p>Thanks!</p>