通过angular 6访问rest-api时出现CORS错误

时间:2019-08-20 12:57:57

标签: rest angular6

通过angular6访问rest-api时出现CORS错误

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);
    })
  }

}

在控制台上出现错误

SEC7120:[CORS]在'http://localhost:4200上的跨域资源的Access-Control-Allow-Origin响应标头中未找到源'http://localhost:4200''http://localhost:8080/restapiapp/webapi/profiles' '。

错误:ProgressEvent

标题:对象 消息:“ http://localhost:8080/restapiapp/webapi/profiles的Http故障响应:0未知错误” 名称:“ HttpErrorResponse” 好的:错误 状态:0 statusText:“未知错误” 网址:“ http://localhost:8080/restapiapp/webapi/profiles

原始:对象

1 个答案:

答案 0 :(得分:0)

仅通过添加CrossOrigin批注即可工作