小编典典

从文件读取AJAX

ajax

我正在使用AJAX从文本文件中读取。我如何只读取第一行?


阅读 231

收藏
2020-07-26

共1个答案

小编典典

此代码应帮助您从远程文本文件中读取:

var txtFile = new XMLHttpRequest();
txtFile.open("GET", "http://my.remote.url/myremotefile.txt", true);
txtFile.onreadystatechange = function() {
  if (txtFile.readyState === 4) {  // Makes sure the document is ready to parse.
    if (txtFile.status === 200) {  // Makes sure it's found the file.
      allText = txtFile.responseText; 
      lines = txtFile.responseText.split("\n"); // Will separate each line into an array
    }
  }
}
txtFile.send(null);
2020-07-26