title: Angular 6 HttpClient and PHP error Http failure response for (unknown url): 0 Unknown Error
body: <p>I have the following Authentication and Login service in Angular 6:</p>
  
  <pre><code>import { Injectable } from '@angular/core';
  import { HttpClient, HttpHeaders } from '@angular/common/http';
  import { map } from 'rxjs/operators';
  @Injectable({
    providedIn: 'root'
  })
  
  export class AuthApiService {
    public credentials:any=[];
    constructor(private http: HttpClient) { }
  
  
    login(username, password)
    {
      let headerOptions = new HttpHeaders();
      headerOptions.append('Content-Type', 'application/json');
      headerOptions.append("authorization", "basic aGM0YXBwOmHjddP5NjU=");
      let test = {user: username, pass: password};
      this.credentials = JSON.stringify(test);
      console.log("hi "+ this.credentials);
      return this.http.post('https://127.0.0.1/api/scripts/login.php', this.credentials, {
        headers: headerOptions
      }).pipe(map(
          res=&gt;{
            console.log(res)
          },
          err=&gt;
            console.log(err)
        ))
    }
  }
  </code></pre>
  
  <p>And when I click on Login Button:</p>
  
  <pre><code>a id="btn-login"  (click)="login()" class="btn btn-success"&gt;Login &lt;/a&gt;
  </code></pre>
  
  <p>A function in login.component.ts will execute the login function of the Authentication service:</p>
  
  <pre><code>login(){
      this.auth.login("a", "p").subscribe(
        (data)=&gt;{
          console.log(data)
        },
        (error)=&gt;{
          console.log(error)
        }
      )
  }
  </code></pre>
  
  <p>In php, and for the url, I am running it in the browser and I can see the result properly <code>1</code>, but there is an error at the console when I click on the button:</p>
  
  <blockquote>
    <p>"Http failure response for (unknown url): 0 Unknown Error"</p>
  </blockquote>
  
  <p><a href="https://i.stack.imgur.com/LqTpL.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/LqTpL.jpg" alt="enter image description here"></a></p>
  
  <p>At the network tab:</p>
  
  <p><a href="https://i.stack.imgur.com/7Cq76.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/7Cq76.jpg" alt="enter image description here"></a></p>
  
  <p>The PHP script:</p>
  
  <p>Lets start with the main class:</p>
  
  <pre><code>&lt;?php
  header('Access-Control-Allow-Origin: *');
  header('Content-Type: application/json');
  header('Access-Control-Allow-Methods: GET, POST, PUT, PATCH, DELETE, HEAD, OPTIONS');
  header('Access-Control-Allow-Headers: *');
  
  error_reporting(E_ALL);
  
  class api {
      private $username ="...";
      private $password ="...";
      private $db="...";
      private $host = "...";
  
      public $conn;
  
      //Connection
  
      public function connection(){
          try{
              $this-&gt;conn = new PDO("mysql:host=$this-&gt;host;dbname=$this-&gt;db", $this-&gt;username, $this-&gt;password);
              $this-&gt;conn-&gt;setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
              $this-&gt;conn-&gt;exec("SET CHARACTER SET utf8");
  
              return $this-&gt;conn;
          }
          catch(PDOException $e){
              return $e-&gt;getMessage();
          }
  
      }
  
      //Login
  
      public function login($conn, $user){
          $login = "SELECT username FROM login WHERE username=:user LIMIT 1";
          $execLogin = $this-&gt;conn-&gt;prepare($login);
          $execLogin-&gt;bindValue(":user", $user);
          $execLogin-&gt;execute();
          $res = $execLogin-&gt;rowCount();
  
          return $res;
          // if($res&gt;0)
          // {
          //     return json_encode($res);
          // }
          // else{
          //     echo 0;
          // }
      }
  
  }
  
  ?&gt;
  </code></pre>
  
  <p>And then, the login function is executed (<code>https://127.0.0.1/api/scripts/login.php</code>):</p>
  
  <pre><code>&lt;?php
  header('Content-Type: application/json');
  
  require_once('../api.php');
  
  //Getting username and password from Ionic
  $user="brital";
  $pass = json_decode($_POST['credentials']);
  
  $newApi = new api();
  $conn = $newApi-&gt;connection();
  $res = $newApi-&gt;login($conn, $user);
  echo $res;
  ?&gt;
  </code></pre>