首页 文章

Angular 5上的HTTP标头

提问于
浏览
1

我是新手,我认为这个问题对你来说很容易,但请帮忙 .

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

阻止跨源请求:同源策略禁止在http:// localhost:9999 /读取远程资源 . (原因:缺少CORS Headers 'Access-Control-Allow-Origin') .

有我的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);});
  }
}

我已经尝试了很多变种..但是没有人不适合我......有些人可以提供一些解决它的例子......请...

1 回答

  • 1

    这是服务器而不是客户端的问题 . 您的浏览器阻止您作为安全措施拨打其他主机 . 如果您有权访问服务器,则需要添加:

    Access-Control-Allow-Origin: *
    

    否则,您将需要一个代理来调用服务器

    见:Origin is not allowed by Access-Control-Allow-Origin

相关问题