使用javascript逐行读取文本文件

使用javascript逐行读取文本文件,javascript,asp.net,Javascript,Asp.net,我试图从一个文本文件中读取这种形式的行; 34.925,150.977 35.012,151.034 34.887150.905 我目前正在尝试使用这种方法,但显然不起作用。任何帮助都将不胜感激 var ltlng = []; var txtFile = new XMLHttpRequest(); txtFile.open("GET", "C:\Gmap\LatLong\Coordinates.txt", true); txtFile.onreadystatechange = f

我试图从一个文本文件中读取这种形式的行; 34.925,150.977 35.012,151.034 34.887150.905

我目前正在尝试使用这种方法,但显然不起作用。任何帮助都将不胜感激

var ltlng = [];
var txtFile = new XMLHttpRequest();
    txtFile.open("GET", "C:\Gmap\LatLong\Coordinates.txt", true);
    txtFile.onreadystatechange = function() {
        if (txtFile.readyState === 4) {  
        if (txtFile.status === 200) {  // Makes sure it's found the file.
           lines = txtFile.responseText.split("\n"); // separate each line into an array
             ltlng.push(new google.maps.LatLng(lines.split(",")[0],lines.split(",")[1]); //create the marker icons latlong array
            }
        }
    }
XMLHttpRequest在通过HTTP/S协议而不是文件协议调用资源时工作,如您的示例所示


因此,为了使代码正常工作,您应该将此代码与web服务器一起尝试

,因为这可能会导致同源策略。您需要从同一个域而不是本地filetry进行ajax调用file:///C:/Gmap/LatLong/Coordinates.txt 而不是C:\Gmap\LatLong\Coordinates.txtunforty,这对我来说不起作用。我将尝试使用jquery,因为它对文本文件的读取更加友好