我对新的CoinMarketCap API充满信心。 :(
以下是Node中的请求示例。如何在Angular中提出请求? 有什么建议么? 谢谢。
enter code here
/* Example in Node.js ES6 using request-promise, concepts should translate
to your language of choice */
const rp = require('request-promise');
const requestOptions = {
method: 'GET',
uri: 'https://pro-api.coinmarketcap.com/v1/cryptocurrency/listings/latest',
qs: {
start: 1,
limit: 5000,
convert: 'USD'
},
headers: {
'X-CMC_PRO_API_KEY': 'API_KEY_HERE'
},
json: true,
gzip: true
};
rp(requestOptions).then(response => {
console.log('API call response:', response);
}).catch((err) => {
console.log('API call error:', err.message);
});
答案 0 :(得分:0)
getAllCoinsListing() {
const httpOptions = {
headers: new HttpHeaders({
'X-CMC_PRO_API_KEY': 'API_KEY_HERE'
})
};
return this.http.get(this.apiUrl, httpOptions);
}
答案 1 :(得分:-1)
根据找到的文档here,您无法从Web客户端执行此HTTP调用:
Making HTTP requests on the client side with Javascript is currently prohibited through CORS configuration. This is to protect your API Key which should not be visible to users of your application so your API Key is not stolen. Secure your API Key by routing calls through your own backend service.
一种解决方案是创建自己的后端API。然后,该API可以执行对Coinmarketcap的HTTP调用。然后,您的网站将与您定制的后端API通信。