Jump to content

How to handle json respond from api endpoint (Uncaught TypeError: )


Recommended Posts

My problem is how to handle this json respond using document.getElementById or any other methods. I needed to output the value of 'P'. And also work with php echo on the output
The json respond is bellow

[
  {
    "a": 26129,        
    "p": "0.01633102",  
    "q": "4.70443515",  
    "f": 27781,         
    "l": 27781,     
    "T": 1498793709153,  
  "m": true,          
    "M": true           
  }
]

Bellow is the code I'm working with. it gives Uncaught TypeError: Cannot read property 'p' of undefined

<!DOCTYPE html>
<html>
<body>

<h2> Price</h2>

<p id="demo"></p>

<script>
 var burl = "https://api3.binance.com"; /////////// baseurl/////////
    
    var query = '/api/v3/aggTrades'; 
    
    query += '?symbol=BTCUSDT';
    
    var url = burl + query;
    
    var ourRequest = new XMLHttpRequest();
    
    ourRequest.open('GET',url,true);
    ourRequest.onload = function(){
        console.log(ourRequest.responseText);
    }
    var obj = ourRequest.send();	
	document.getElementById("demo").innerHTML = obj.p;
	
</script>
</body>
</html>

Please help me and thanks in advance 

Link to post
Share on other sites

You have an array containing an object.

Array
(
    [0] => stdClass Object
        (
            [a] => 26129
            [p] => 0.01633102
            [q] => 4.70443515
            [f] => 27781
            [l] => 27781
            [T] => 1498793709153
            [m] => 1
            [M] => 1
        )

)

Try innerHTML = obj[0].p

Edited by Barand
Link to post
Share on other sites

 

1 hour ago, charlion said:

@Barand Thanks for your respond. i used  



 document.getElementById("demo"). innerHTML = obj[0].p

still Uncaught TypeError: Cannot read property '0' of undefined.

 more Light into it would be appreciated 

 

If you are not using Fetch then you need to parse the data:

var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function() {
  if (this.readyState == 4 && this.status == 200) {
    var myObj = JSON.parse(this.responseText);
    document.getElementById("demo").innerHTML = myObj.name;
  }
};
xmlhttp.open("GET", "json_demo.txt", true);
xmlhttp.send(); 

 

Edited by Strider64
  • Thanks 1
Link to post
Share on other sites

try

<html>
<head>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.5.0/jquery.min.js"></script>
<script type='text/javascript'>
$().ready( function() {
    var burl = "https://api3.binance.com"; /////////// baseurl/////////
    var query = burl + '/api/v3/aggTrades';
    
    $.get(
        query,
        {"symbol":"BTCUSDT"} ,
        function(resp) {
            $("#demo").html("a - " +   resp[0].a + "<br>")
            $("#demo").append("p - " + resp[0].p + "<br>")
            $("#demo").append("q - " + resp[0].q + "<br>")
            var d = new Date()
            d.setTime(resp[0].T)
            $("#demo").append("T - " + d.toString())
        },
        "JSON"
    )
})    
</script>
</head>
<body>
    <div id='demo'>
        <!--output goes here-->
    </div>
</body>
</html>

outputs...

a - 577817177
p - 48585.69000000
q - 0.10000000
T - Mon Feb 15 2021 17:50:19 GMT+0000 (GMT Standard Time)

 

  • Thanks 1
Link to post
Share on other sites
2 hours ago, Barand said:

try


<html>
<head>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.5.0/jquery.min.js"></script>
<script type='text/javascript'>
$().ready( function() {
    var burl = "https://api3.binance.com"; /////////// baseurl/////////
    var query = burl + '/api/v3/aggTrades';
    
    $.get(
        query,
        {"symbol":"BTCUSDT"} ,
        function(resp) {
            $("#demo").html("a - " +   resp[0].a + "<br>")
            $("#demo").append("p - " + resp[0].p + "<br>")
            $("#demo").append("q - " + resp[0].q + "<br>")
            var d = new Date()
            d.setTime(resp[0].T)
            $("#demo").append("T - " + d.toString())
        },
        "JSON"
    )
})    
</script>
</head>
<body>
    <div id='demo'>
        <!--output goes here-->
    </div>
</body>
</html>

outputs...


a - 577817177
p - 48585.69000000
q - 0.10000000
T - Mon Feb 15 2021 17:50:19 GMT+0000 (GMT Standard Time)

 

Thanks 

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.