title: How to solve "Access-Control-Allow-Origin" issue in Angular2 + Lite-Server?
body: <p>I have an Angular 2 application that requests data from an external API.</p>
  
  <p>I can't change the API code, but I'm able to change the TypeScripts and the Lite-Server configuration.</p>
  
  <p>Error:
  <strong>XMLHttpRequest cannot load <a href="http://api.zanox.com/" rel="nofollow noreferrer">http://api.zanox.com/</a>.... No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin '<a href="http://localhost:4000" rel="nofollow noreferrer">http://localhost:4000</a>' is therefore not allowed access.</strong></p>
  
  <p>I already saw a lot of content about CORS but I don't know how to adapt that to my code. What's the easiest way to solve that?</p>
  
  <p>My service:</p>
  
  <pre><code>import { Injectable } from '@angular/core';
  import { Http, Response, Headers, RequestOptions } from '@angular/http';
  import { Observable } from 'rxjs/Observable';
  import { Page } from './page';
  import 'rxjs/add/operator/map';
  import 'rxjs/add/operator/catch';
  
  @Injectable()
  export class ProductService {
  
      private urlPage = 'http://api.zanox.com/json/...';
  
      constructor(private http: Http) { }
  
      getPage(): Observable&lt;Page&gt; {
          return this.http.get(this.urlPage).map(this.extractData).catch(this.handleError);
      }
  
      private extractData(res: Response) {
          let body = res.json();
          return body || {};
      }
  
      private handleError(error: any) {
          let errMsg = (error.message) ? error.message :
              error.status ? `${error.status} - ${error.statusText}` : 'Server error';
          console.error(errMsg);
          return Observable.throw(errMsg);
      }
  
  }
  </code></pre>
  
  <p>My component:</p>
  
  <pre><code>import { Component, OnInit } from '@angular/core';
  import { ProductService } from './product/productService';
  import { Page } from './product/page';
  
  @Component({
      templateUrl: 'app/app.product.html',
      selector: 'product-app',
      providers: [ProductService]
  })
  export class AppProduct implements OnInit {
  
      private errorMessage: string;
  
      page: any;
  
      constructor(
          private productService: ProductService) {
      }
  
      ngOnInit() {
          this.getPage();
      }
  
      getPage() {
          this.productService.getPage().subscribe(
              page =&gt; this.page = page,
              error =&gt; this.errorMessage = &lt;any&gt;error
          )
      }
  }
  </code></pre>