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();
});