好像ajax本来就可以读到.txt
-
-
<html>
-
-
<head>
-
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
-
<title>读取txt文件</title>
-
</head>
-
-
<body>
-
<script src="https://cdn.bootcss.com/jquery/2.2.4/jquery.js"></script>
-
<script>
-
$.ajax({
-
url: "txt.txt",
-
success: function(data, status) {
-
console.log(arguments)
-
console.log(data)
-
},
-
error: function(data, status) {
-
console.log(arguments)
-
}
-
});
-
-
/**/
-
-
function readTxt() {
-
var xhr = new XMLHttpRequest();
-
xhr.open("get", "txt.txt", true);
-
xhr.send();
-
xhr.onreadystatechange = function() {
-
if(xhr.readyState == 4 && xhr.status == 200) {
-
console.log(xhr);
-
console.log(xhr.responseText);
-
} else if(xhr.status == 404) {
-
console.log(xhr.status);
-
}
-
};
-
}
-
readTxt()
-
</script>
-
</body>
-
-
</html>