AJAX - Leer contenido de archivo local con Ajax (sin servidor).

 
Vista:
Imágen de perfil de ScriptShow
Val: 18
Ha mantenido su posición en AJAX (en relación al último mes)
Gráfica de AJAX

Leer contenido de archivo local con Ajax (sin servidor).

Publicado por ScriptShow (10 intervenciones) el 10/01/2020 11:20:07
Saludos,

Ajax y JavaScript Nativo, nos ofrecen opciones para leer el contenido de un archivo de texto, sin necesidad de un servidor local y/o externo. Solo utiliza scripts y lenguajes del lado cliente.

El ejemplo:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
<!DOCTYPE html>
 
<html>
<head>
<title>AJAX</title>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<meta http-equiv="X-UA-Compatible"content="IE=edge" />
</head>
<body>
<script>
var Texto;
 
function test(file) {
if (window.ActiveXObject) {
var archivo = new ActiveXObject("Microsoft.XMLHTTP");
}
else
if (window.XMLHttpRequest) {
var archivo = new XMLHttpRequest();
}
 
archivo.open("GET", file, false);
archivo.onreadystatechange = function () {
if (archivo.readyState == 4) {
if (archivo.status == 200 || archivo.status == 0) {
var Texto = archivo.responseText;
alert("Contenido: \n" + Texto);
}
}
}
archivo.send(null);
}
</script>
 
<input type="button" value="T e s t" onClick="test('ajax.txt')">
</body>
</html>

Espero sea útil.
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
1
Responder