I want to obtain the data from an external JSON file in the same directory into my js portion of HTML file.
I've set up a local nodejs server rendering the referenced html file. I tried to use jQuery's $.getJSON
method, and console.log()
the referenced JSON file but nothing shows up in chrome console.
$.getJSON('http://localhost:3000/test.json', function(data) {
console.log(data);
});
To test, instead of my local JSON file, I've inputted a URL to json data, and it does display the url information. So maybe I do not the proper pathing of the test.json
file?
Here's the server portion:
const http = require('http')
const fs = require('fs')
const port = 3000
const server = http.createServer(function(req,res){
res.writeHead(200, {'Content-Type': 'text/html'})
fs.readFile('index.html', function(error,data){
if (error){
res.writeHead(404)
res.write('Error: File Not Found')
} else {
res.write(data)
}
res.end()
})
})
server.listen(port, function(error){
if(error){
console.log('An error has occured: ', error)
} else {
console.log('Listening on port '+ port)
}
})