BAG LION BAG LION -4 years ago 139
Javascript Question

xmlhttprequest,fetch() whatever works

I'be been pouring over stackoverflow for about a week now and I just can't find a solution to my problem. I've scoured youtube for tutorials and have about 8 books out from the library. So far, most, if not all sources have only vague explanations of what needs to be done.

I'm simply trying to pull ticker data from an API and display it on the screen. I've typed out examples from stackoverflow, dummies guides, SAMS, youtube tutorials.... nothing works. I'm aware of the same-origin policy but I'm not sure if its what is causing all the error. I click the button and nothing shows up. I turned off my security in Chrome and Firefox....still nothing.

Here's a simple script I copied out of Javascript for dummies. I've modified it to use the api I want and the api returns this data:

"id": "honey",
"name": "Honey",
"symbol": "HONEY",
"rank": "369",
"price_usd": "0.240062",
"price_btc": "0.00012133",
"24h_volume_usd": "652.439",
"market_cap_usd": "16398.0",
"available_supply": "68306.0",
"total_supply": "68306.0",
"percent_change_1h": "2.05",
"percent_change_24h": "-15.47",
"percent_change_7d": "-70.84",
"last_updated": "1495298087"

I just want the price in USD to show up on my webpage.

I would appreciate any help, I usually prefer figuring things out for myself but I'm afraid I've been stumped on this for a week solid. This is just one example, I am completely open to ANY code that works.
Here is my script:

<title>Displaying JSON Data</title>

<script type="text/javascript">
function init(e)P
function reqListener(){
var obj = JSON.parse(this.responseText);


function documentLoader(){
var oReq = new XMLHttpRequest();
oReq.onload = reqListener;"GET", "",true);

<form id="myForm">
<button id="myButton" type="button">Click to load</button>
<h1>Honey Coin Price:</h1>
<div id="price_usd"></div>

Answer Source

You have several typos, this seems to be working fine:


function init(e) {

function reqListener() {
    var obj = JSON.parse(this.responseText);
    document.getElementById('price_usd').innerHTML= obj[0].price_usd;

function documentLoader(){
    var oReq = new XMLHttpRequest();
    oReq.onload = reqListener;"GET", "",true); 
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download