title: Invalid Request type Angular2
body: <p>Can you tell me where I'm wrong? When I use the Postman then it's working.But why I cannot do the same using Angular2? Here the backend api is from PHP.I have never used PHP backend before.Is that different than normal ASP.net Web Api? I mean the way we have to send the parameters and all...</p>
  
  <p><strong>Service.ts</strong></p>
  
  <pre><code>import { Injectable } from '@angular/core';
  import { Http, RequestOptions, Headers, Response } from '@angular/http';
  import { Observable } from 'rxjs/Observable';
  import 'rxjs/add/operator/map';
  import 'rxjs/add/operator/catch';
  
  @Injectable()
  export class AuthenticationData {
    authenticationEndPoint: string = "https://www.myk.com/admin/index.php?route=api/login";
  
    constructor(public http: Http) {
  
    }
  
    //to login
    loginUser(username: string, password: string): Observable&lt;any&gt; {
      let headers = new Headers();
      headers.append('content-type', 'application/json');
  
     /*let body = {
       username: username,
       password: password,
     }*/ Not working this too :(
  
      let body='username=myname&amp;password=admin';//I tried hardcode value.But not working 
  
      let options = new RequestOptions({ headers: headers });
  
      return this.http.post(this.authenticationEndPoint, body, options)
        .map(this.extractData)
        .catch(this.handleError);
  
    }
  
    private extractData(res: Response) {
      let body = res.json();
      return body || {};
    }
  
    private handleError(error: Response | any) {
      let errMsg: string;
      if (error instanceof Response) {
        const body = error.json() || '';
        const err = body.error || JSON.stringify(body);
        errMsg = `${error.status} - ${error.statusText || ''} ${err}`;
      } else {
        errMsg = error.message ? error.message : error.toString();
      }
      console.error(errMsg);
      return Observable.throw(errMsg);
    }
  
  }
  </code></pre>
  
  <p><strong>login.ts</strong></p>
  
  <pre><code>//to login
    loginUser(): void {
      if (this.loginForm.valid) {
        this.authenticationData.loginUser(this.loginForm.value.username, this.loginForm.value.password).subscribe(
          data =&gt; {
            this.response = data;
          },
          err =&gt; {
            console.log(err);
          },
          () =&gt; console.log('Complete')
        );
      }
    }
  </code></pre>
  
  <p><strong>Error:</strong></p>
  
  <blockquote>
    <p>body: "{"error":"Invalid Request type","status":"201"}", status: 200,
    ok: true, statusText: "OK",</p>
  </blockquote>
  
  <p><strong>Php:</strong></p>
  
  <pre><code>&lt;?php
  class ControllerApiLogin extends Controller {
   private $error = array();
  
   public function index() {
    $json = array();
  
    if (($this-&gt;request-&gt;server['REQUEST_METHOD'] == 'POST') &amp;&amp; !empty($this-&gt;request-&gt;get['username']) &amp;&amp; !empty($this-&gt;request-&gt;get['password'])) {
  
     if(!empty($this-&gt;request-&gt;get['username']) &amp;&amp; !empty($this-&gt;request-&gt;get['password'])){
  
      $this-&gt;load-&gt;language('common/login');
  
      $this-&gt;document-&gt;setTitle($this-&gt;language-&gt;get('heading_title'));
  
      // User
      $this-&gt;registry-&gt;set('user', new Cart\User($this-&gt;registry));
  
      if ($this-&gt;validate()) {
  
       $token = token(32);
  
       $token_count = $this-&gt;user-&gt;getUniqueToken($token);
  
       if($token_count==0)
       {
        $this-&gt;session-&gt;data['token'] = $token; 
       }else{
  
        $token = token(32);
  
        $token_count = $this-&gt;user-&gt;getUniqueToken($token);
  
        $this-&gt;session-&gt;data['token'] = $token;  
       }
  
       $this-&gt;load-&gt;model('user/user');
  
       $user_info = $this-&gt;model_user_user-&gt;getUserByEmail($this-&gt;request-&gt;get['username']);
  
       $tokeninfo = array();
  
       if(count($user_info) &gt; 0){
  
        $tokeninfo = array(
         'token' =&gt; $token,
         'user_id' =&gt; $user_info['user_id'],
         'ip'  =&gt; $this-&gt;request-&gt;server['REMOTE_ADDR']
        );
  
        $date_expired = $this-&gt;model_user_user-&gt;addUserapitoken($tokeninfo);
  
       }else{
        $date_expired = '';
       }
  
       $json['token'] = $token;
       $json['date_expired'] = $date_expired;
       $json['status'] = '200';
      }else{
       $json['error'] = "No match for Username and/or Password.";
       $json['status'] = '201';
      }
  
     }else{
      $json['error'] = 'Something Went Wrong!!! &lt;br&gt; PLease Enter Correct Login Credentials!!!';
      $json['status'] = '201';
     }
  
     //$this-&gt;response-&gt;addHeader('Content-Type: application/json');
     //$this-&gt;response-&gt;setOutput(json_encode($json));
    }
    else{
  
      $json['error'] = 'Invalid Request type';
      $json['status'] = '201';
    }
    if (isset($this-&gt;request-&gt;server['HTTP_ORIGIN'])) {
     $this-&gt;response-&gt;addHeader('Access-Control-Allow-Origin: ' . $this-&gt;request-&gt;server['HTTP_ORIGIN']);
     $this-&gt;response-&gt;addHeader('Access-Control-Allow-Methods: GET, PUT, POST, DELETE, OPTIONS');
     $this-&gt;response-&gt;addHeader('Access-Control-Max-Age: 1000');
     $this-&gt;response-&gt;addHeader('Access-Control-Allow-Headers: Content-Type, Authorization, X-Requested-With');
    }
  
    $this-&gt;response-&gt;addHeader('Content-Type: application/json');
    $this-&gt;response-&gt;addHeader('HTTP/1.1'.$json['status']);
    $this-&gt;response-&gt;setOutput(json_encode($json));
   }
  
   protected function validate() {
  
    //$this-&gt;registry-&gt;set('user', new Cart\User($this-&gt;registry));
  
    if (!isset($this-&gt;request-&gt;get['username']) || !isset($this-&gt;request-&gt;get['password']) || !$this-&gt;user-&gt;login($this-&gt;request-&gt;get['username'], html_entity_decode($this-&gt;request-&gt;get['password'], ENT_QUOTES, 'UTF-8'))) {
     $this-&gt;error['warning'] = $this-&gt;language-&gt;get('error_login');
    }
  
    return !$this-&gt;error;
   }
  }
  </code></pre>