My node and express code is thus:
var express = require("express");
var app = express();
app.get('/',function(request,response){
console.log('Received');
response.header("Access-Control-Allow-Origin", "*");
response.header('Content-Type', 'text/plain');
response.send("Get Handled");
console.log(response.headersSent);
});
app.listen(3000, () => console.log('Listening?'));
The javascript on my calling page goes thusly (stripping out what I believe to be irrelevant):
function writeResponseText(myRequest){
console.log("onreadystatechange")
console.log(myRequest.status)
console.log(myRequest.readyState)
console.log(myRequest.responseText)
if(myRequest.readyState === XMLHttpRequest.DONE && myRequest.status === 200) {
mainSection.innerHTML = myRequest.responseText;
}
}
function getRESTResponse() {
var url = "http://localhost:3000";
var myRequest = new XMLHttpRequest();
myRequest.addEventListener("load", transferResult);
myRequest.addEventListener("error", transferResult2);
myRequest.onload = writeResponseText2(myRequest);
myRequest.open("GET", url);
myRequest.setRequestHeader("Content-Type", "text/plain");
myRequest.onreadystatechange = writeResponseText(myRequest);
myRequest.send();
}
The problem I am having is that readyState never gets to 4. onload seems to fire when readyState changes to 1. My HTML is never updated.
The 'load' event does eventually trigger, but I'm not sure how to pass the response object to it, if that's how you're supposed to deal with it. All the examples I've found seem to still be using onreadystatechange, but it only seems to fire once, while readyState is still 0. Am I supposed to set the listener up again somehow?
The node/express examples I've found are likewise very simple, so I'm guessing I'm missing a whole layer of how it works?
I can't find any specific example of the issue I'm having, so I guess I'm derping in some unique manner, or missing a fine detail.
Basically I just want to be able to talk to myself, at this point... send myself the most simple GET possible, and handle it accordingly.
I am prepared to kick myself. Please help :)