0

app.component.ts:

import { Component } from '@angular/core';
import { Http, Response } from '@angular/http';
import 'rxjs/add/operator/map';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent {
  title = 'app will work!!';
  private apiUrl = 'https://address-book-demo.herokuapp.com/api/contacts';
  data: any = {};
  constructor(private http: Http){
  console.log('hellooooooo');
  this.getContacts();
  this.getData();
  }

  getData(){
  return this.http.get(this.apiUrl)
   .map((res: Response) => res.json())
  }
  getContacts(){
   this.getData().subscribe(data => {
    console.log(data);
    this.data = data;
   })
  }
}

I am trying hit the above apiurl and get the data in my console.it is working fine. But, When i change the url as "https://www.getpostman.com/collections/010e61af1......." then i am not able to hit that. (it is giving cross origin issue).

Once, i install CORS plugin it is working fine.

How, can i do that without CORS plugin.

and i am very new to work with postman api.

sowmya
  • 51
  • 2
  • 8

1 Answers1

0

You need to add CORS properties for response headers

res.header('Access-Control-Allow-Origin', '*');
res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE,OPTIONS');
res.header('Access-Control-Allow-Headers', 'Content-Type, Authorization, Content-Length, X-Requested-With');

The simplest way to do it on nodejs as follows:

var cors = require('cors'); app.use(cors());

Please look at here for details

Mike Adamenko
  • 2,944
  • 1
  • 15
  • 28