从 .txt 文件获取数据到 .js 文件
Getting the data from a .txt file into a .js file
我们需要将信息从 .txt 文件中提取到 .js 文件中。
我们已经尝试使用 'Fetch API' 但没有成功,有没有其他方法可以解决这个问题。
我们正在使用 Node.js 服务器来查看发生了什么
这是 .txt 文件的内容
2min = 2:00
2min away = 2:00
ScoreAway = 7
ScoreHome = 8
Time = 30:00
halvleg = 2nd
服务器端使用Node Js,客户端使用fs。
在浏览器中,您可以使用 XMLHttpRequest。像这样:
function readTxtFile(file)
{
var rawFile = new XMLHttpRequest();
rawFile.open("GET", "neuro.txt", false);
rawFile.onreadystatechange = function ()
{
if(rawFile.readyState === 4)
{
if(rawFile.status === 200 || rawFile.status == 0)
{
let allText = rawFile.responseText;
console.log(allText);
}
}
}
rawFile.send(null);
}
readTxtFile();
我们需要将信息从 .txt 文件中提取到 .js 文件中。 我们已经尝试使用 'Fetch API' 但没有成功,有没有其他方法可以解决这个问题。
我们正在使用 Node.js 服务器来查看发生了什么
这是 .txt 文件的内容
2min = 2:00
2min away = 2:00
ScoreAway = 7
ScoreHome = 8
Time = 30:00
halvleg = 2nd
服务器端使用Node Js,客户端使用fs。 在浏览器中,您可以使用 XMLHttpRequest。像这样:
function readTxtFile(file)
{
var rawFile = new XMLHttpRequest();
rawFile.open("GET", "neuro.txt", false);
rawFile.onreadystatechange = function ()
{
if(rawFile.readyState === 4)
{
if(rawFile.status === 200 || rawFile.status == 0)
{
let allText = rawFile.responseText;
console.log(allText);
}
}
}
rawFile.send(null);
}
readTxtFile();