This question already has answers here:
</div>
</div>
<div class="grid--cell mb0 mt4">
<a href="/questions/14028959/why-does-jquery-or-a-dom-method-such-as-getelementbyid-not-find-the-element" dir="ltr">Why does jQuery or a DOM method such as getElementById not find the element?</a>
<span class="question-originals-answer-count">
(9 answers)
</span>
</div>
<div class="grid--cell mb0 mt8">Closed <span title="2017-03-21 18:59:10Z" class="relativetime">3 years ago</span>.</div>
</div>
</aside>
I am doing a AJAX call pulling JSON data from a specific site but for some reason I am receiving and error before the call.
Error message:
Uncaught TypeError: Cannot read property 'addEventListener' of null at script.js:2
Code:
var ajaxtest = document.getElementById("dmeo");
ajaxtest.addEventListener("click",function() {
var ourRequest = new XMLHttpRequest();
ourRequest.open("GET","https://learnwebcode.github.io/json-example/animals-1.json")//post if you want to send data
ourRequest.onload = function(){
// console.log(ourRequest.responseText);//test logging to console.
var ourData = JSON.parse(ourRequest.responseText);
console.log(ourData[0]);
};
ourRequest.send();
});
</div>