开发者

AJAX reading from file

开发者 https://www.devze.com 2023-02-19 15:38 出处:网络
I\'m reading from a text file using AJAX. How do开发者_Python百科 I read only the first line?This code should help you read from a remote text file:

I'm reading from a text file using AJAX. How do开发者_Python百科 I read only the first line?


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


It depends on how you output the file on the back-end. Depending on a language, be it PHP, Java or other, you can read the first line of your file and output it to the response.

To find out whether a file has changed: HTTP code 304 and browser-side caching may help in this case.

0

精彩评论

暂无评论...
验证码 换一张
取 消