jQuery ajax - two separate API requests with combined results

0 votes

Evening Internet World,

First post here so be gentle, i've learnt a lot from this site thus far.. and now I seek help..

I've tried a few variations of code like $when & $then, function in function, but I can't get the result I seek, it doesn't combine results. So thought best to ask away here and show my code.. Please help..

html

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<div class="row" id="stats">
  <div class="col-4 col-12-large">
    <h4><strong>Ether contributed</strong></h4>
    <span id="eth_balance" style="font-size: 2.5em;">&mdash;</span>
    <!--<p id="total-ether-message" style="font-size:11px;"></p>-->
  </div>
  <div class="col-4 col-12-large">
    <h4><strong>Contributions in USD</strong></h4>
    <span id="token_usd" style="font-size: 2.5em;">&mdash;</span>
    <!--<p id="total-usd-message" style="font-size:11px;"></p>-->
  </div>
  <div class="col-4 col-12-large">
    <h4><strong>Tokens issued</strong></h4>
    <span id="token_amount" style="font-size: 2.5em;">&mdash;</span>
    <!-- <p id="total-tokens-message" style="font-size:11px;"></p> -->
  </div>
</div>

js

var token = '0xa74476443119A942dE498590Fe1f2454d7D4aC0d';
var address = '0xda0aed568d9a2dbdcbafc1576fedc633d28eee9a';

$.get("https://api.tokenbalance.com/token/" + token + "/" + address +'', function(data) {
    $("#eth_balance").html(Math.round(data.eth_balance).toFixed(2) + " ETH");
    $("#token_amount").html(Math.round(data.balance).toFixed(2) + " " + data.symbol);
});

$.get("https://api.etherscan.io/api?module=stats&action=ethprice", function(data) {
    $("#token_usd").html("$ " + Math.round(data.result.ethusd).toFixed(2));
    // Ideally I'd like to get [ data.result.ethusd x data.eth_balance ] to replace #token_usd, all wrapped in one function
    alert(data.result.ethusd)
});
Jun 19, 2018 in Blockchain by charlie_brown
• 7,720 points
1,967 views

1 answer to this question.

0 votes

You can move the second ajax call inside the first, and store values from data in a variable that will be available to the inner function too.

Like so (second call won't work here or in a fiddle):

    var token = '0xa74476443119A942dE498590Fe1f2454d7D4aC0d';
    var address = '0xda0aed568d9a2dbdcbafc1576fedc633d28eee9a';

    $.get("https://api.tokenbalance.com/token/" + token + "/" + address + '', function (data) {
        var eth_balance = data.eth_balance;
        $("#eth_balance").html(Math.round(data.eth_balance).toFixed(2) + " ETH");
        $("#token_amount").html(Math.round(data.balance).toFixed(2) + " " + data.symbol);
        $.get("https://api.etherscan.io/api?module=stats&action=ethprice", function (data) {
            $("#token_usd").html("$ " + Math.round(data.result.ethusd * eth_balance).toFixed(2));
            // Ideally I'd like to get [ data.result.ethusd x data.eth_balance ] to replace #token_usd, all wrapped in one function
            alert(data.result.ethusd)
        });
    });
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<div class="row" id="stats">
    <div class="col-4 col-12-large">
        <h4>
            <strong>Ether contributed</strong>
        </h4>
        <span id="eth_balance" style="font-size: 2.5em;">&mdash;</span>
        <!--<p id="total-ether-message" style="font-size:11px;"></p>-->
    </div>
    <div class="col-4 col-12-large">
        <h4>
            <strong>Contributions in USD</strong>
        </h4>
        <span id="token_usd" style="font-size: 2.5em;">&mdash;</span>
        <!--<p id="total-usd-message" style="font-size:11px;"></p>-->
    </div>
    <div class="col-4 col-12-large">
        <h4>
            <strong>Tokens issued</strong>
        </h4>
        <span id="token_amount" style="font-size: 2.5em;">&mdash;</span>
        <!-- <p id="total-tokens-message" style="font-size:11px;"></p> -->
    </div>
</div>
answered Jun 19, 2018 by aryya
• 7,460 points

Related Questions In Blockchain

0 votes
1 answer
+2 votes
2 answers

Can two chaincode interact with each other?

Yes, it is possible for two chaincodes ...READ MORE

answered Jul 12, 2018 in Blockchain by slayer
• 29,370 points
2,336 views
0 votes
1 answer

Korbit api with python http.client

http.client.HTTPSConnection accepts hostname, not a url. conn = http.client.HTTPSConnection("api.korbit.co.kr", ...READ MORE

answered Aug 28, 2018 in Blockchain by digger
• 26,740 points
1,398 views
0 votes
2 answers

Passing AJAX GET results to location

Set a variable in global scope and ...READ MORE

answered Sep 4, 2018 in Blockchain by eddy
686 views
0 votes
1 answer

Truffle tests not running after truffle init

This was a bug. They've fixed it. ...READ MORE

answered Sep 11, 2018 in Blockchain by Christine
• 15,790 points
1,900 views
+1 vote
1 answer
0 votes
1 answer

Hyperledger Sawtooth vs Quorum in concurrency and speed Ask

Summary: Both should provide similar reliability of ...READ MORE

answered Sep 26, 2018 in IoT (Internet of Things) by Upasana
• 8,620 points
1,422 views
+1 vote
2 answers
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP