0

I'm really new to AJAX. How can I get the value of id, symbol, name etc. and put it in my table? I tried everything even JSON.stringify but I could not find this question anywhere so...

API output: [{"id":"90","symbol":"BTC","name":"Bitcoin","nameid":"bitcoin","rank":1,"price_usd":"9813.14","percent_change_24h":"10.04","percent_change_1h":"1.80","percent_change_7d":"5.56","market_cap_usd":"180330472483.63","volume24":"38774320414.79","volume24_native":"3951263.62","csupply":"18376421.00","price_btc":"1.00","tsupply":"18376421","msupply":"21000000"}]

Here is the HTML code.
Note: class="info" is just for testing. I will assign unique id's to the td's when it works correctly

<body>
        <h1>
            Live cryptocurrency data
        </h1>
        <table>
            <tr>
                <td>
                    Symbol
                </td>
                <td class="info">
                    -
                </td>
            </tr>
            <tr>
                <td>
                    Price USD
                </td>
                <td class="info">
                    -
                </td>
            </tr>
            <tr>
                <td>
                    24h Volume USD
                </td>
                <td class="info">
                    -
                </td>
            </tr>
            <tr>
                <td>
                    Market Cap USD
                </td>
                <td class="info">
                    -
                </td>
            </tr>
            <tr>
                <td>
                    Current supply
                </td>
                <td>
                    -
                </td>
            </tr>
            <tr>
                <td>
                    Total supply
                </td>
                <td>
                    -
                </td>
            </tr>
            <tr>
                <td>
                    Max supply
                </td>
                <td>
                    -
                </td>
            </tr>
            <tr>
                <td>
                    Percentage change 1h
                </td>
                <td>
                    -%
                </td>
            </tr>
            <tr>
                <td>
                    Percentage change 24h
                </td>
                <td>
                    -%
                </td>
            </tr>
            <tr>
                <td>
                    Percentage change 7d
                </td>
                <td>
                    -%
                </td>
            </tr>
        </table>
        <br>
        <button>
            Refresh Bitcoin data
        </button>
    </body>

Here is the Javascript code

$("button").click
(
    function ()
    {
        $.ajax
        (
            {
                url: "https://api.coinlore.net/api/ticker/?id=90",
                method: "GET",
                success: function(result)
                        {
                            $(".info").text(JSON.stringify(result));
                        }
            }
        )
    }
);
Lucas
  • 27
  • 5

1 Answers1

0

Your API is returning an array with, in this case, 1 single object. Try result[0].id

Also, use dataType: 'json' in your Ajax options as per:

Parse JSON from JQuery.ajax success data

Abraham Labkovsky
  • 1,771
  • 6
  • 12