我是新手,我认为这个问题对你来说很简单,但请帮忙。
我需要在加载页面上发出 http 请求,它可以正常工作,但我的查询被消息阻止了
Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at http://localhost:9999/. (Reason: CORS header ‘Access-Control-Allow-Origin’ missing).
这是我的 Angular 代码:
export class AppComponent implements OnInit {
public apiURL:string = 'http://localhost:9999/';
public logged: boolean;
response: any;
constructor(private elementRef:ElementRef, private http:Http) {
this.logged = this.elementRef.nativeElement.getAttribute('auth');
}
ngOnInit() {
this.checkLogin()
}
checkLogin() {
this.response = this.http.post(this.apiURL, {"search": "search"}, ).map(res => res.json()).subscribe(data => {console.log(data);});
}
}
我已经尝试了很多变体..但没有人不适合我...有人可以提供解决它的小例子吗...拜托...
最佳答案
这是服务器上的问题,而不是客户端上的问题。作为安全措施,您的浏览器阻止您调用其他主机。如果您有权访问您需要添加的服务器:
Access-Control-Allow-Origin: *
否则你将需要一个代理来调用服务器
关于angular - Angular 5 上的 HTTP header ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47892996/