Repository navigation
Expand file tree
/
Copy pathJavascript_7-12_JQuery-Ajax.html
More file actions
118 lines (108 loc) · 4.29 KB
/
Copy pathJavascript_7-12_JQuery-Ajax.html
File metadata and controls
118 lines (108 loc) · 4.29 KB
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<script src="js/jquery-3.1.1.min.js"></script>
<style>
</style>
<script>
$(document).ready(inicio);
function inicio() {
$("#ajax").click(function () {
var nom = $("#nombre").val();
var ape = $("#apellido").val();
var parametros = {
"nombre": nom,
"apellido": ape
};
//$.ajax: tiene un conjunto completo de parámetros
$.ajax({
url: "saludo.php", //URL donde realizamos la petición
data: parametros, //Datos a enviar, como objeto o como cadena
success: function (respuesta) { //Función si la petición ha ido bien
$("#mostrar").text(respuesta);
},
error: function (xhr, status) { //Función si la petición ha fallado
alert("Ha ocurrido un error");
},
complete: function (xhr, status) {
alert("Petición realizada");
}
//Otras opciones: beforeSend, async, cache, context, headers... (ver API)
});
});
//Otros métodos: basados en $.ajax pero tienen parámetros establecidos por defecto
$("#enviarGet").click(function () {
$.get("saludo.php", {
"nombre": "Ada",
"apellido": "Lovelace"
}, function (respuesta) {
$("#mostrar").text(respuesta);
});
});
$("#enviarGetMensajes").click(function () {
//Función que no envía nada y controla diferentes resultados de la solicitud
$.get("saludo.php", function () {
alert("Exito");
}).done(function () {
alert("Exito 2");
}).fail(function () {
alert("Error");
}).always(function () {
alert("Siempre");
});
});
$("#enviarPost").click(function () {
//Función que envía datos pero no devuelve nada
$.post("saludo.php", {
"nombre": "Ada",
"apellido": "Lovelace"
});
//Función que no envía datos pero recibe respuesta
$.post("holamundo.php", function (respuesta) {
alert("Respuesta: " + respuesta);
});
//Función que envía datos y recibe respuesta
$.post("saludo.php", {
"nombre": "Ada",
"apellido": "Lovelace"
}).done(function (respuesta) {
alert("Respuesta: " + respuesta)
});
});
//Añade un script y ejecuta una función del script
$("#getScript").click(function () {
$.getScript("script.js", function () {
dentroScript();
})
});
//Obtiene un JSON desde el servidor
$("#getJSON").click(function () {
$.getJSON("json.php", function (respuesta) {
$.each(respuesta, function (clave, valor) {
alert(clave + " : " + valor);
});
});
});
//Carga datos del servidor y el HTML lo introduce en un elemento
$("#load").click(function () {
$("#mostrar").load("holamundo.php", function () {
alert("Se ha cargado holamundo.php en #mostrar");
});
});
}
</script>
</head>
<body>
<input type="text" name="nombre" id="nombre" />
<input type="text" name="apellido" id="apellido" />
<button id="ajax">Enviar Ajax</button>
<button id="enviarGet">Enviar Get</button>
<button id="enviarGetMensajes">Enviar Get</button>
<button id="enviarPost">Enviar Post</button>
<button id="getScript">getScript</button>
<button id="getJSON">getJSON</button>
<button id="load">Usar Load</button>
<p id="mostrar"></p>
</body>
</html>