AJAX reading from file

前端 未结 2 1552
余生分开走
余生分开走 2021-01-16 14:38

I\'m reading from a text file using AJAX. How do I read only the first line?

2条回答
  •  生来不讨喜
    2021-01-16 15:00

    This code should help you read from a remote text file:

    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);
    

提交回复
热议问题