[英]Ajax API Call access data on success call back, JQUERY
为什么在成功回叫后无法访问价格等数据。 我得到了成功的回应。 我得到了聚合数据,但是以某种方式无法访问该数组[aggregatedData]中的价格值。 尝试了一切
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');
})
}
如果您正在寻找价格:
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');
})
}
API的响应是一个包含响应,消息和数据的对象。 因此,您需要像这样循环遍历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');
})
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.