在以角度发送Web API http请求时,状态代码403出现CORS错误

时间:2017-09-17 19:06:54

标签: angular http get cors http-status-code-403

以下是我在向特定API端点发送请求时收到的错误消息:

  

XMLHttpRequest无法加载   https://poloniex.com/public?command=returnCurrencies。没有   '访问控制允许来源'标题出现在请求的上   资源。起源' http://localhost:4200'因此是不允许的   访问。 响应的HTTP状态代码为403

以下是我尝试运行以向该API端点发送请求的代码:

import { Component, OnInit } from '@angular/core';
import { Headers, Http } from '@angular/http';
import 'rxjs/add/operator/toPromise';
import 'rxjs/add/operator/map';

@Component({
  selector: 'app-home',
  templateUrl: './home.component.html',
  styleUrls: ['./home.component.css'],
})
export class HomeComponent implements OnInit {

  results;

  constructor(private http: Http) { }

  ngOnInit() {
    this.results = this.http.get('https://poloniex.com/public?
command=returnCurrencies')
      .map(res => res.json());
    this.results.subscribe(data => console.log(data));

  } 
}

请求失败独立于API,因为我也在其他网站上尝试过。

0 个答案:

没有答案