为什么我无法在回拨成功时访问价格等数据。我得到了成功的回应。我得到了聚合数据但不知何故无法访问该数组[aggregateatedData]中的price值。尝试了一切
function reqBitcoinData() {
$.ajax({
"url":"https://www.cryptocompare.com/api/data/coinsnapshot/?fsym=BTC&tsym=USD",
"method": "get",
"cache": false,
"dataType": "JSON"
}).done(function(jData) {
console.log(jData);
$.each(jData, function(i, jCurrency) {
var AggregatedData = jCurrency.AggregatedData;
aCurrencies.push(AggregatedData);
console.log(AggregatedData);
})
}).fail(function(jFail) {
console.log('Failed');
})
}
答案 0 :(得分:0)
如果您正在寻找价格:
function reqBitcoinData() {
aCurrencies=[];
$.ajax({
"url":"https://www.cryptocompare.com/api/data/coinsnapshot/?fsym=BTC&tsym=USD",
"method": "get",
"cache": false,
"dataType": "JSON"
}).done(function(jData) {
console.log(jData);
var AggregatedData=jData.Data.AggregatedData;
console.log(AggregatedData);
//Price under AggregatedData
var Price=AggregatedData.PRICE;
console.log(Price);
//Prices under Exchanges
$.each(jData.Data.Exchanges, function(i, jCurrency) {
aCurrencies.push(jCurrency.PRICE);
})
console.log(aCurrencies);
}).fail(function(jFail) {
console.log('Failed');
})
}
答案 1 :(得分:-1)
API的响应是一个包含Response,Message和Data的对象。所以你需要循环遍历response.data,如下所示:
function reqBitcoinData() {
$.ajax({
"url":"https://www.cryptocompare.com/api/data/coinsnapshot/?
fsym=BTC&tsym=USD",
"method": "get",
"cache": false,
"dataType": "JSON"
}).done(function(jData) {
console.log(jData);
$.each(jData.Data, function(i, jCurrency) {
var AggregatedData = jCurrency.AggregatedData;
aCurrencies.push(AggregatedData);
console.log(AggregatedData);
})
}).fail(function(jFail) {
console.log('Failed');
})
}