Test Foro de elhacker.net SMF 2.1

Programación => Desarrollo Web => Mensaje iniciado por: franfis en 23 Mayo 2013, 17:16 PM

Título: Ayuda con ajax
Publicado por: franfis en 23 Mayo 2013, 17:16 PM
Hola amigos, este código lo saque de un tutorial, pero no se en que parte está mal porque no me funciona.

tuna.html<!DOCTYPE html>
<html>
<head>
<script type ="text/javascript" src ="tuna.js"></script>
</head>
<body onload="process()">
Here is a list of people...
<br/>
<br/>
<div id="theD"/>
</body>
</html>


tuna.xml<?xml version ="1.0" encoding="UTF-8" standalone="yes" ?>
<response>
<people>
<person>
<name>
Antonio garcia
</name>
<ssn>
sdffggh4456678
</ssn>
<name>
Anid gutierrez
</name>
<ssn>
s34566776783785
</ssn>
</person>
</people>
</response>


tuna.jsvar xmlHttp = createXmlHttpRequestObject();

//create object
function createXmlHttpRequestObject(){
var xmlHttp;
if(window.XMLHttpRequest){
xmlHttp = new XMLHttpRequest();
}else{
xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
}
return xmlHttp;
}
//called onLoad
function process(){
if(xmlHttp){
try{
xmlHttp.open("GET", "tuna.xml" true);
xmlHttp.onreadystatechange = handleStateChange;
xmlHttp.send(null);
}catch(e){
alert(e.toString());
}
}
}

//when state changes
function handleStateChange(){
if(xmlHttp.readyState==4){
if(xmlHttp.status==200){
try{
handleResponse();
}catch(e){
alert(e.toString());
}
}else{
alert(xmlHttp.statusText);
}
}
}

// handle the reponse from the server
function handleResponse(){
var xmlResponse = xmlHttp.responseXML;
root = xmlResponse.documentElement;
names = root.getElementsByTagName("name");
ssns = root.getElementsByTagName("ssn");

var stuff = "";
for(var i=0; i < names.length; i++){
stuff = names.item(i).firstChild.data + " - " + ssns.item(i).firstChild.data + "<br/>";
}

theD = document.getElementById("theD");
theD.innerHTML = stuff;
}


El resultado debe ser mas o menos así:

Here is a list of people...

Antonio garcia - sdffggh4456678
Anid gutierrez - s34566776783785
Título: Re: Ayuda con ajax
Publicado por: #!drvy en 23 Mayo 2013, 20:26 PM
El div no se pone así. es:
Código (html4strict) [Seleccionar]
<div id="theD"></div>


En el javascript en la linea 17, despues de "tuna.xml" tiene que haber una coma (,).
Código (javascript) [Seleccionar]
xmlHttp.open("GET", "tuna.xml", true);


La linea 50 del js se sobre-escriben los nombres que va sacando y al final solo te mostrara un nombre. Has de añadir un + antes del = para añadir el siguente valor a la variable.
Código (javascript) [Seleccionar]
stuff += names.item(i).firstChild.data + " - " + ssns.item(i).firstChild.data + "<br/>";

Copiar&Pegar es malo para la salud.

Saludos

Título: Re: Ayuda con ajax
Publicado por: franfis en 23 Mayo 2013, 22:44 PM
Gracias drvy, en el primero, se ve mucho mejor como lo escribiste, aunque también funcione igual.

En el tercer error, es curioso que así le haya funcionado en el video tutorial http://www.youtube.com/watch?v=Kd-ZS0NCAJw (http://www.youtube.com/watch?v=Kd-ZS0NCAJw)

CitarCopiar&Pegar es malo para la salud
Tienes razon  ;D