【发布时间】:2018-06-20 13:00:16
【问题描述】:
我有一个关于加密市场币安的问题。 他们有公共 api,尽管我可以在 Angular 中使用它来创建交易应用程序。
但是我有一些麻烦。 在 chrome 中使用该链接我得到 json 结果。 https://api.binance.com/api/v1/exchangeInfo
但使用 Angular 4 httpClient:
this.http.get('https://api.binance.com/api/v1/exchangeInfo').subscribe(res => console.log(res));
我有错误:跨域请求被阻止:同源策略不允许读取 api.binance.com/api/v1/exchangeInfo 上的远程资源。 (原因:CORS 标头“Access-Control-Allow-Origin”缺失)
它不起作用。我不明白,为什么我不能在 Angular 应用程序中使用该 API?https://github.com/binance-exchange/binance-official-api-docs/blob/master/rest-api.md
我该怎么办? 我应该这样设置标题:
getMarkets() {
const headers = new HttpHeaders();
headers.set('Content-Type', 'application/json');
headers.set('Accept', 'application/json');
headers.set('Access-Control-Allow-Headers', 'Content-Type');
headers.set('Access-Control-Allow-Origin', '*');
const path = 'https://api.binance.com/api/v1/exchangeInfo';
return this.http.get(path, {headers: headers});
}
提前致谢
【问题讨论】:
-
这里没有足够的细节来开始猜测。考虑使用与 HTTP 请求良好对应的异步技术(即 not RxJS)。
-
为什么不用 RxJS?你能解释一下吗?
-
RxJS 在您有零个或多个结果的流时很有用。使用 HTTP 请求,您总是有 一个 结果。它很笨拙且容易出错,并且会丢弃可用的language level facilities。
标签: javascript angular http typescript cors