Angular 5上的HTTP头

Angular 5上的HTTP头,angular,http,angular5,Angular,Http,Angular5,我是新来的,我想这个问题对你来说很容易,但是请帮助我 我需要在加载页面上发出http请求,它正在工作,但我的查询被消息阻止 已阻止跨源请求:同一源策略不允许读取 位于的远程资源 . (原因:缺少CORS标头“访问控制允许原点”) 这是我的代码: export class AppComponent implements OnInit { public apiURL:string = 'http://localhost:9999/'; public logged: boolean; r

我是新来的,我想这个问题对你来说很容易,但是请帮助我

我需要在加载页面上发出http请求,它正在工作,但我的查询被消息阻止

已阻止跨源请求:同一源策略不允许读取 位于的远程资源 . (原因:缺少CORS标头“访问控制允许原点”)

这是我的代码:

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 cli创建了应用程序,那么它是从服务器端创建的。您需要允许所有源站都来自服务器端。您是否使用nodejs创建http服务提供程序?如果可以,也许可以共享服务器端代码。omg。。泰。。。我一直试图在客户端发现问题