【发布时间】:2020-01-10 00:07:45
【问题描述】:
在我的 Angular 应用程序中,当我需要通过基本身份验证从我的 API 进行 GET 调用时,我在我的 JS 控制台中收到此错误:
从源“http://localhost:4200”访问“*********”处的 XMLHttpRequest 已被 CORS 策略阻止:对预检请求的响应未通过访问控制检查:无“访问权限” -Control-Allow-Origin' 标头存在于请求的资源上。
如果我尝试使用 Postman,同样的调用不会有任何问题。我该如何解决? 这是我的代码:
export class DataService {
constructor(private http: HttpClient) { }
private postsURL = "********";
getData(){
const httpOptions = {
headers: new HttpHeaders({
'Access-Control-Allow-Origin': '*',
'Access-Control-Expose-Headers': 'Content-Length,X-Foo,X-Bar',
'Content-Type': 'application/json',
'Authorization': 'Basic ' + btoa('*****:*****')
})
};
return this.http.get(this.postsURL, httpOptions);
}
}
【问题讨论】:
标签: angular rest api http cors