No me muestra un json por consola
Publicado por leonardo (9 intervenciones) el 16/08/2018 21:55:36
Buenas tardes... Aprendiendo Ajax estoy tratando de mostrar un sencillo json por consola pero no logro hacerlo. Se que debe ser algo minimo pero no se que puede estar fallando, He revisado linea a linea y ya no se que mas hecerle. Adjunto el codigo:
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
<!DOCTYPE html>
<html lang="en">
<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>Document</title>
</head>
<body>
<button onclick="ejecutarAjax()">Mostrar Datos</button>
<script>
function ejecutarAjax(){
var xmlhttp;
if(window.XMLHttpRequest){
xmlhttp=new XMLHttpRequest();
}else{
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function(){
if(xmlhttp.readyState==4 && xmlhttp.status==200){
let respuesta=JSON.parse(xmlhttp.responseText);
console.log(respuesta);
}
}
xmlhttp.open("GET","datos.json",true);
xmlhttp.send();
}
</script>
</body>
</html>
1
2
3
4
5
6
//datos.json
{
"nombre":"leonardo",
"edad":35
}
Valora esta pregunta
0