简体   繁体   English

成功回叫JQUERY时的Ajax API调用访问数据

[英]Ajax API Call access data on success call back, JQUERY

Why I can´t access Data like price, etc on the success of the call back. 为什么在成功回叫后无法访问价格等数据。 I get success response. 我得到了成功的回应。 I get the aggregated Data but somehow can´t access the value of price inside that array[aggregatedData]. 我得到了聚合数据,但是以某种方式无法访问该数组[aggregatedData]中的价格值。 Tried everything 尝试了一切

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');
        })
    }

If you are looking for Prices : 如果您正在寻找价格:

  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');
    })
}

The response from the API is an object that contains Response, Message and Data. API的响应是一个包含响应,消息和数据的对象。 So you need to loop through the response.data like so: 因此,您需要像这样循环遍历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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM