Skip to content

Commit 214bc6e

Browse files
committed
JavaScript / Ajax / Json
1 parent 462d73b commit 214bc6e

File tree

2 files changed

+77
-1
lines changed

2 files changed

+77
-1
lines changed

Ejemplos/AjaxURL.html

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8">
5+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
6+
<meta http-equiv="X-UA-Compatible" content="ie=edge">
7+
<title>Document</title>
8+
</head>
9+
<body>
10+
11+
<a href="#" id="cargar">Cargar</a>
12+
<div id="listado"></div>
13+
14+
<script>
15+
document.getElementById('cargar').addEventListener('click',cargarArray);
16+
17+
function cargarArray(e){
18+
e.preventDefault();
19+
const xhr = new XMLHttpRequest();
20+
xhr.open('GET','https://jsonplaceholder.typicode.com/posts',true);
21+
xhr.onload = function(){
22+
if(this.status === 200){
23+
24+
const results = JSON.parse(this.responseText);
25+
let htmlResult ='';
26+
results.forEach(function(result) {
27+
htmlResult += `
28+
<ul>
29+
<li><h3>userId : ${result.userId}</h3></li>
30+
<li>id : ${result.id}</li>
31+
<li>title : ${result.title}</li>
32+
<li>body : ${result.body}</li>
33+
</ul>
34+
`;
35+
})
36+
document.getElementById('listado').innerHTML = htmlResult;
37+
}
38+
}
39+
xhr.send();
40+
}
41+
</script>
42+
43+
</body>
44+
</html>

README.md

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -824,8 +824,40 @@ class Casa {
824824
xhr.send();
825825
}
826826
```
827+
828+
## Ejemplo de una URL y Ajax
829+
>### HTML
830+
```html
831+
<a href="#" id="cargar">Cargar</a>
832+
<div id="listado"></div>
833+
```
834+
>### JavaScript
835+
```js
836+
document.getElementById('cargar').addEventListener('click',cargarArray);
837+
838+
function cargarArray(e){
839+
e.preventDefault();
840+
const xhr = new XMLHttpRequest();
841+
xhr.open('GET','https://uinames.com/api/?maxlen=75',true);
842+
xhr.onload = function(){
843+
if(this.status === 200){
844+
//console.log(JSON.parse(this.responseText));
845+
//document.getElementById('listado').innerHTML = `${this.responseText}`;
846+
847+
this.responseText.foreach(function(result){
848+
document.getElementById('listado').innerHTML += `${result}`;
849+
})
850+
851+
852+
}
853+
}
854+
xhr.send();
855+
}
856+
```
857+
858+
827859
---
828-
# JAVA SCRIPT FUNCIONES VARIAS
860+
# JavaScript FUNCIONES VARIAS
829861

830862
>## Seleccionar un elemento del DOM
831863
```js

0 commit comments

Comments
 (0)