I have a server using node and express. I'm building the client app. It's supposed to get data from the server like so:
let req = new XMLHTTPRequest();
req.open('GET', 'localhost:5000/newarticle');
req.onload = function() {
let data = JSON.parse(req.responseText);
renderHTML(data);
}
req.send();
This code is part of a main.js file which is linked to my index.html file. The index.html file is served by express to the client when they access the website. On index.html, there's a button, with functionality associated with the onclick event that's defined in main.js. So far, so good. But when the button is clicked, I get a console message in the browser reading "XMLHttpRequest is not defined."
My understanding is that the browser (Chrome/V8) and node are separate JavaScript environments. Things like XMlHttpRequest, which are defined in node, are not defined in V8.
This is confusing to me. No tutorial I find on how to use AJAX mentions this as an issue. I haven't been able to find a source that digs into how I can serve up an HTML + javascript page that allows the user to send AJAX requests back to the server that addresses this issue. Honestly, I don't even know quite what to search for to try and find an explanation for this set of issues. Any advice or links would be very appreciated!