0

Getting CORS error while accessing rest-api through angular6

import { Component, OnInit } from '@angular/core';
import { TestService } from '../../test.service';
import { HttpClient } from '@angular/common/http';

@Component({
  selector: 'app-view-component',
  templateUrl: './view-component.component.html',
  styleUrls: ['./view-component.component.css']
})
export class ViewComponentComponent implements OnInit {

  username:string="";
  response:any;
  public httpOptions : any;

  constructor(private http: HttpClient) {
  }

  ngOnInit() {
  }

  search(){
    this.http.get('http://localhost:8080/restapiapp/webapi/profiles')
    .subscribe((response)=>{
      this.response=response;
      console.log("Got the response");
      console.log(this.response);
    })
  }

}

getting error on my console

SEC7120: [CORS] The origin 'http://localhost:4200' did not find 'http://localhost:4200' in the Access-Control-Allow-Origin response header for cross-origin resource at 'http://localhost:8080/restapiapp/webapi/profiles'.

error: ProgressEvent

headers: Object message: "Http failure response for http://localhost:8080/restapiapp/webapi/profiles: 0 Unknown Error" name: "HttpErrorResponse" ok: false status: 0 statusText: "Unknown Error" url: "http://localhost:8080/restapiapp/webapi/profiles"

proto: Object

mayur singhal
  • 77
  • 1
  • 2
  • 10

1 Answers1

0

it is working by just adding CrossOrigin annotation

mayur singhal
  • 77
  • 1
  • 2
  • 10