jQuery: read text file from file system

前端 未结 10 2484
再見小時候
再見小時候 2020-11-30 10:09

I am trying to read a text file using jquery, like this:

// LOAD file and split line by line and append divs
$.get(\'myFile.txt\', function(data) {    
    v         


        
10条回答
  •  醉梦人生
    2020-11-30 10:41

    This is working fine in firefox at least.

    The problem I was facing is, that I got an XML object in stead of a plain text string. Reading an xml-file from my local drive works fine (same directory as the html), so I do not see why reading a text file would be an issue.

    I figured that I need to tell jquery to pass a string in stead of an XML object. Which is what I did, and it finally worked:

    function readFiles()
    {
        $.get('file.txt', function(data) {
            alert(data);
        }, "text");
    }
    

    Note the addition of '"text"' at the end. This tells jquery to pass the contents of 'file.txt' as a string in stead of an XML object. The alert box will show the contents of the text file. If you remove the '"text"' at the end, the alert box will say 'XML object'.

提交回复
热议问题