angular - Angular 5 上的 HTTP header

标签 angular http angular5

我是新手,我认为这个问题对你来说很简单,但请帮忙。

我需要在加载页面上发出 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: *

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

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

关于angular - Angular 5 上的 HTTP header ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47892996/

相关文章:

javascript - Auth0 与 Angular 4 登录错误

具有根级导入的 Angular2 应用程序模块

java - 在 Java 中执行 get 请求会导致 HTTP 响应代码 : 415

api - 404 是否是当前不存在的资源的正确返回代码?

html - 无法更改选项卡的高度,Angular 6 中的颜色

Angular 6 多库交叉依赖

angular - 如何在 Angular4 中使用 Karma-Jasmine 测试导出功能

html - 如何在 HTML 中通过 ngFor(角度 5)创建 100 x 100 的表格?

http - Cookie 是否安全,不会通过 http 劫持

javascript - 类型错误 : Cannot read property 'location' of undefined