elhacker.net cabecera Bienvenido(a), Visitante. Por favor Ingresar o Registrarse
¿Perdiste tu email de activación?.

 

 


Tema destacado: (TUTORIAL) Aprende a emular Sentinel Dongle By Yapis


+  Foro de elhacker.net
|-+  Programación
| |-+  Desarrollo Web (Moderador: #!drvy)
| | |-+  Ayuda con ajax
0 Usuarios y 1 Visitante están viendo este tema.
Páginas: [1] Ir Abajo Respuesta Imprimir
Autor Tema: Ayuda con ajax  (Leído 1,839 veces)
franfis

Desconectado Desconectado

Mensajes: 245



Ver Perfil
Ayuda con ajax
« 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
Código:
<!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
Código:
<?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.js
Código:
var 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


En línea

#!drvy
Moderador
***
Desconectado Desconectado

Mensajes: 5.850



Ver Perfil WWW
Re: Ayuda con ajax
« Respuesta #1 en: 23 Mayo 2013, 20:26 pm »

El div no se pone así. es:
Código
  1. <div id="theD"></div>

En el javascript en la linea 17, despues de "tuna.xml" tiene que haber una coma (,).
Código
  1. 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
  1. stuff += names.item(i).firstChild.data + " - " + ssns.item(i).firstChild.data + "<br/>";

Copiar&Pegar es malo para la salud.

Saludos



« Última modificación: 23 Mayo 2013, 20:42 pm por drvy | BSM » En línea

franfis

Desconectado Desconectado

Mensajes: 245



Ver Perfil
Re: Ayuda con ajax
« Respuesta #2 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

Citar
Copiar&Pegar es malo para la salud
Tienes razon  ;D
En línea

Páginas: [1] Ir Arriba Respuesta Imprimir 

Ir a:  

Mensajes similares
Asunto Iniciado por Respuestas Vistas Último mensaje
Ayuda con Ajax
Desarrollo Web
000luis 2 1,864 Último mensaje 30 Mayo 2013, 17:52 pm
por 000luis
Ayuda con Ajax y Paginacion PHP
Desarrollo Web
000luis 0 1,802 Último mensaje 8 Junio 2013, 18:22 pm
por 000luis
Ayuda con Chat AJAX
Desarrollo Web
Mokonauta 2 2,125 Último mensaje 15 Agosto 2013, 08:09 am
por Mokonauta
Ayuda con ajax y php
Desarrollo Web
edwarditos 5 2,204 Último mensaje 20 Octubre 2013, 11:12 am
por edwarditos
Ayuda con ajax y php
Desarrollo Web
itzg3 2 1,516 Último mensaje 12 Abril 2014, 21:21 pm
por herminio59
WAP2 - Aviso Legal - Powered by SMF 1.1.21 | SMF © 2006-2008, Simple Machines