问题描述
我想从同一目录中的外部JSON文件中获取数据到HTML文件的js部分中。
我已经设置了一个本地的nodejs服务器来呈现引用的html文件。我尝试使用jQuery的$.getJSON
方法和console.log()
所引用的JSON文件,但在Chrome控制台中什么都没显示。
$.getJSON('http://localhost:3000/test.json',function(data) {
console.log(data);
});
为了测试,我没有输入本地JSON文件,而是输入了指向json数据的URL,并且它确实显示了url信息。所以也许我没有正确test.json
文件的路径?
这是服务器部分:
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)
}
})
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)