-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathajax03.html
61 lines (51 loc) · 1.88 KB
/
ajax03.html
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
<!DOCTYPE html>
<html lang="es">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Ajax Tutorial 3</title>
</head>
<body>
<center>
<h1>Tutorial 3 AJAX</h1>
<h3>Mostrar JSON Array</h3>
<button onclick="ajax_get_json()">Mostrar Array JSON</button>
<div id="info"></div>
</center>
<script type="text/javascript">
/* Variable para llamar al document getElementById con info tener el div en una variable */
var resultado = document.getElementById('info');
function ajax_get_json(){
var xmlhttp;
if (window.XMLHttpRequest){
xmlhttp = new XMLHttpRequest();
} else {
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange = function(){
if(xmlhttp.readyState == 4 && xmlhttp.status == 200){
//Mostrar la informacion en una ventana de texto
//alert(xmlhttp.responseText);
//Guardo la información del JSON en una variable
//El JSON.parse lo que hace es convertir la respuesta en texto del servidor en un objeto
var datos = JSON.parse(xmlhttp.responseText);
var j = 0;
if (resultado.innerHTML === ""){
for (var i in datos) {
j +=1;
resultado.innerHTML += j + ".- " + datos[i].nombre + " "
+ datos[i].apellido + " tiene "
+ datos[i].edad + " Años" +"<br/>";
}
}
}
}
//Abro el archivo que contiene los datos del array en Json
xmlhttp.open("GET", "array.json", true);
// Envio la respuesta
xmlhttp.send();
}
</script>
</body>
</html>