【发布时间】:2019-12-25 16:57:17
【问题描述】:
通过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”在“http://localhost:8080/restapiapp/webapi/profiles”的跨源资源的 Access-Control-Allow-Origin 响应标头中找不到“http://localhost:4200”。
错误:进度事件
标题:对象 消息:“http://localhost:8080/restapiapp/webapi/profiles 的 Http 失败响应:0 未知错误” 名称:“HttpErrorResponse” 好的:假的 状态:0 statusText:“未知错误” 网址:“http://localhost:8080/restapiapp/webapi/profiles”
原型:对象
【问题讨论】:
-
您的 API 是否支持 CORS?这可能不是
angular HttpClient问题 -
如何将 CORS 添加到我的 api 中??
-
你的 API 是用 NodeJs、.Net、Spring 构建的吗? ...?那应该是另一个问题?
-
它在 spring-boot 上