Webapp/page is able to load data requested from local express api when client/web browser is run from localhost (the server running the express app)- however when pointing a browser from another machine on the network at the server's IP:port to view the webapp, I get an error when the ajax fires:
"failed to load resource ::netERR_CONNECTION_REFUSED"
EDIT: The dev tools in chrome were vague and did not provide much insight into the issue (for me anyway). When viewing same in firefox the network tab of dev tools made issue obvious:
"Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at localhost:1337/getDeviceStatusAllCached"
So it was a CORS issue- answer below is what fixed it for me.
relevant code: app.js-
app.get('/', function(req, res){
logger.printWriteLine('Issues tracker (re)loaded.', 0);
res.sendFile(path.join(__dirname, '/views/wallboard.html'));
})
app.get('/getDeviceStatusAllCached', (req, res) => {
logger.printWriteLine('Front-end requesting cached data.');
reqs.retrieveCachedDeviceStatusAll(obj => res.send(obj));
});
reqs.js -
retrieveCachedDeviceStatusAll: function (callback) {
fs.readFile('./data/getDeviceStatusAll.json', 'utf-8', function(err,data){
if(err) throw err;
obj = JSON.parse(data);
console.log(JSON.stringify(data));
callback(obj);
});
}
wallboard.html
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<table ... working table stuff>
</table>
<script>
var dataArray = [];
console.log('running ajax');
$.ajax({
method: 'GET',
url: 'http://localhost:1337/getDeviceStatusAllCached',
dataType: "json",
success:function(response){
dataArray = response
buildTable(dataArray)
console.log(dataArray)
}
})
</script>