charlion Posted February 15, 2021 Share Posted February 15, 2021 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 Quote Link to comment https://forums.phpfreaks.com/topic/312153-how-to-handle-json-respond-from-api-endpoint-uncaught-typeerror/ Share on other sites More sharing options...
Barand Posted February 15, 2021 Share Posted February 15, 2021 (edited) 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 February 15, 2021 by Barand Quote Link to comment https://forums.phpfreaks.com/topic/312153-how-to-handle-json-respond-from-api-endpoint-uncaught-typeerror/#findComment-1584478 Share on other sites More sharing options...
requinix Posted February 15, 2021 Share Posted February 15, 2021 The send method does not return a value. Forget XMLHttpRequest and learn about the magic that is fetch. Quote Link to comment https://forums.phpfreaks.com/topic/312153-how-to-handle-json-respond-from-api-endpoint-uncaught-typeerror/#findComment-1584479 Share on other sites More sharing options...
charlion Posted February 15, 2021 Author Share Posted February 15, 2021 @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 Quote Link to comment https://forums.phpfreaks.com/topic/312153-how-to-handle-json-respond-from-api-endpoint-uncaught-typeerror/#findComment-1584480 Share on other sites More sharing options...
Strider64 Posted February 15, 2021 Share Posted February 15, 2021 (edited) 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 February 15, 2021 by Strider64 1 Quote Link to comment https://forums.phpfreaks.com/topic/312153-how-to-handle-json-respond-from-api-endpoint-uncaught-typeerror/#findComment-1584484 Share on other sites More sharing options...
Barand Posted February 15, 2021 Share Posted February 15, 2021 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) 1 Quote Link to comment https://forums.phpfreaks.com/topic/312153-how-to-handle-json-respond-from-api-endpoint-uncaught-typeerror/#findComment-1584490 Share on other sites More sharing options...
charlion Posted February 15, 2021 Author Share Posted February 15, 2021 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 Quote Link to comment https://forums.phpfreaks.com/topic/312153-how-to-handle-json-respond-from-api-endpoint-uncaught-typeerror/#findComment-1584492 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.