通过 angular 访问 rest-api 时出现 CORS 错误 6
Getting CORS error while accessing rest-api through angular 6
通过 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' 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”。
错误:ProgressEvent
headers: Object
消息:"Http failure response for http://localhost:8080/restapiapp/webapi/profiles: 0 Unknown Error"
姓名:"HttpErrorResponse"
好的:假的
状态:0
状态文本:"Unknown Error"
url:“http://localhost:8080/restapiapp/webapi/profiles”
原型: Object
只需添加 CrossOrigin 注释即可工作
通过 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' 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”。
错误:ProgressEvent
headers: Object 消息:"Http failure response for http://localhost:8080/restapiapp/webapi/profiles: 0 Unknown Error" 姓名:"HttpErrorResponse" 好的:假的 状态:0 状态文本:"Unknown Error" url:“http://localhost:8080/restapiapp/webapi/profiles”
原型: Object
只需添加 CrossOrigin 注释即可工作