AJAX - Ajax con respuesta XML-Segundo Intento

 
Vista:
sin imagen de perfil

Ajax con respuesta XML-Segundo Intento

Publicado por Victor (29 intervenciones) el 14/06/2017 10:05:41
Buenos dias.
Tengo una pagina en la que se introdoce un nombre y debe responder si es nombre esta disponible y en caso de no estarlo mostrar unas sugerencias,y aqui es donde falla, que estan en un fichero xml.
Esta es la pagina:
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
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/
xhtml1/DTD/xhtml1-transitional.dtd">
 
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta http-equiv="content-type" content="text/html" charset="iso-8859-1" />
    <title>Ejercicio 14 - Comprobar disponibilidad del login y mostrar alternativas</title>
    <script type="text/javascript">
    var READY_STATE_COMPLETE = 4;
    var peticion_http = null;
 
    function inicializa_xhr()
    {
        if (window.XMLHttpRequest) {
            return new XMLHttpRequest();
        } else if (window.ActiveXObject) {
            return new ActiveXObject("Microsoft.XMLHTTP");
        }
    }
 
    function comprobar() {
        var login = document.getElementById("login").value;
        peticion_http = inicializa_xhr();
        if (peticion_http) {
            peticion_http.onreadystatechange = procesaRespuesta;
            peticion_http.open("POST", "compruebaDisponibilidadEjer14.xml",
            true);
            peticion_http.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
            peticion_http.send("login=" + login + "&nocache=" + Math.random());
        }
    }
    function procesaRespuesta() {
        if (peticion_http.readyState == READY_STATE_COMPLETE) {
            if (peticion_http.status == 200) {
 
                var login = document.getElementById("login").value;
                var documento_xml = peticion_http.responseXML;
                var nombresUsados = documento_xml.getElementsByTagName("login");
                var raiz = documento_xml.getElementsByTagName("respuesta")[0];
                var disponible = raiz.getElementsByTagName("disponible")[0].firstChild.nodeValue;
                if (disponible == "si") {
                    document.getElementById("disponibilidad").innerHTML = "El nombre elegido [" + login + "] está disponible";
                }
                else {
                    var mensaje = "NO está disponible el nombre elegido [" + login + "]. Puedes probar con las siguientes alternativas.";
                    var alternativas = raiz.getElementsByTagName("alternativas")[0];
                    var logins = alternativas.getElementsByTagName("login");
                    mensaje += "<ul>";
                    for (var i = 0; i < logins.length; i++) {
                        mensaje += "<li><a href=\"#\"onclick=\"selecciona('" + logins[i].firstChild.nodeValue + "'); return false\">" + logins[i].firstChild.nodeValue + "<\/a><\/li>";
                    }
                    mensaje += "<\/ul>";
                    document.getElementById("disponibilidad").innerHTML = mensaje;
                }
            }
        }
    }
        function selecciona(login) {
            var cuadroLogin = document.getElementById("login");
            cuadroLogin.value = login;
        }
 
        window.onload = function () {
            document.getElementById("comprobar").onclick = comprobar;
        }
</script>
</head>
<body>
    <h1>Comprobar disponibilidad del login y mostrar alternativas</h1>
    <form>
        <label for="login">Nombre de usuario:</label>
        <input type="text" name="login" id="login" />
        <a id="comprobar" href="#">Comprobar disponibilidad...</a>
    </form>
    <div id="disponibilidad"></div>
</body>
</html>

Y este el fichero XML:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<?xml version="1.0" encoding="utf-8" ?>
 <xml>
  <login>Paco</login>
  <respuesta>
    <disponible>si</disponible>
  </respuesta>
  <respuesta>
    <disponible>no</disponible>
    <alternativas>
      <login>Juan</login>
      <login>Pedro</login>
      <login>Jorge</login>
    </alternativas>
  </respuesta>
</xml>

Me gustaria saber como estraigo del fichero XML el valor de la etiqueta <login> para así hacer la comprobacion
ya que como está ahora no comprueba nada.
Es decir,claramente algo está mal,pero el que?
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder