Skip to content
Advertisement

Show json result in Textbox ASP.Net

I want to know that how can I get the following json result in textbox instead console.log in asp.net C# ?

<script>

        var apikey = {
            key:'XXX'
        }

            request('GET', 'https://pro-api.coinmarketcap.com/v1/global-metrics/quotes/latest?CMC_PRO_API_KEY=' + apikey.key).then((r1) => {
                var x1 = JSON.parse(r1.target.responseText);
                console.log(x1.data.quote.USD.total_market_cap);
        }).catch()

        function request(method, url)
        {
            return new Promise(function (resolve, reject) {
                var xhr = new XMLHttpRequest();
                xhr.open(method, url);
                xhr.onload = resolve;
                xhr.onerror = reject;
                xhr.send();
            });
        }
        </script>

Thx everyone

Advertisement

Answer

Try this:

document.getElementById('textboxId').value = x1.data.quote.USD.total_market_cap;

you can have:

<asp:TextBox ID="textboxId" runat="server"></asp:TextBox>

Sometimes your js code could be executed before the rendering of the asp.net page has been completed, so it is better to add the js to the end of the body, or use jquery to wait the completion of the rendering phase.

$(document).ready(function() {
   document.getElementById('<%=textboxId.ClientID %>').value = x1.data.quote.USD.total_market_cap;
});
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement