I have an external js file with one function that I am trying to test. I cannot get the function to call from a onclick.
I had an init function and I removed the call to that and just called the loadJSON function directly, but no matter what I do I get a reference error.
I looked into these questions. Uncaught exception: ReferenceError: Undefined variable: $
Function is not defined - uncaught referenceerror
neither seemed to help.
function loadJSON(){
var xobj = new XMLHttpRequest();
// must set the mime type
xobj.overrideMimeType("application/json");
// Open the JSON file
xobj.open('GET', 'json/data.json', true);
xobj.onreadystatechange=function(){
if(xobj.readyState == 4 && xobj.status == "200"){
// Required use of an anonymous callback as
// .open will NOT return a value but simply
// returns undefined in asynchronous mode
console.log(xobj.responseText);
}
};
xobj.send(null);
}
https://codepen.io/awschneider-dev/pen/LKKxyq?editors=1010
see codepen for example
All I want is for the JavaScript to pull the Json then I can start saving it in a JavaScript Object so I can dynamically start building a d3 chart with the data. I do use live-server with Node.JS so I can do the xml request on the JSON file, so I know that isnt the issue.
any and all help is appreciated.
Thank you!
****EDIT - My bad, I originally had the () after loadJSON but removed it as a last ditch effort just to see if it would work. I will fix the pen. My apologies"****
****EDIT 2 - I moved the script to be inline, and when I execute in NodeJS Live-Server the line console.log(xobj.responseText);
is returning the entire source code for the html page. Thats interesting! What in the devil is going on here?****
****EDIT 3 - Added screenshot for Techie_28. When I click on the "Unexpected Token <" error for main.js all it does it show all the source code for the html page, as if the html page is somehow being dumped into the .js file. ****
****EDIT 4 - Added screenshot to show what appears when clicking on the "Unexpected token" error. I click on the link to main.js and all it does is show me the index.html page like somehow the html page is being dumped into the .js file. ****