Foros del Web » Programando para Internet » Javascript » Frameworks JS »

problemas con httprequest

Estas en el tema de problemas con httprequest en el foro de Frameworks JS en Foros del Web. hola que tal, tengo problemas para cargar la data de un xml Código: function loadXMLDoc() { var xmlhttp; var txt,x,i; if (window.XMLHttpRequest) {// code for ...
  #1 (permalink)  
Antiguo 07/07/2011, 00:01
 
Fecha de Ingreso: mayo-2011
Mensajes: 42
Antigüedad: 12 años, 11 meses
Puntos: 0
problemas con httprequest

hola que tal, tengo problemas para cargar la data de un xml
Código:
function loadXMLDoc()
{
var xmlhttp;
var txt,x,i;
if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  }
/*else
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }*/

    xmlDoc=xmlhttp.responseXML;
    txt="";
xmlhttp.open("GET","notices.xml",true);
xmlhttp.send();
    x=xmlDoc.getElementsByTagName("RCONTENT");
	
    for (i=0;i<x.length;i++)
      {
      txt=txt + x[i].childNodes[0].nodeValue + "<br />";
      }
    document.getElementById("noticias").innerHTML=txt;

}
Esta es la funcion que carga la informacion del siguiente archivo xml
Código:
<TITTLE>noticia 1</TITTLE>
<RCONTENT>Contenido reducido</RCONTENT>
<CONTENT>Contenido completo</CONTENT>


<TITTLE>noticia 2</TITTLE>
<RCONTENT>Contenido reducido</RCONTENT>
<CONTENT>Contenido completo</CONTENT>
y por ultimo el html es:
Código:
<body onload="loadXMLDoc()">

<div id='content'>

<div id='tittle'></div>

<div id='noticias'>

</div>

</div>
</body>
no sé por qué no carga nada :/ si alguien podría darme una mano le agradecería mucho. gracias !
  #2 (permalink)  
Antiguo 07/07/2011, 06:45
Avatar de maycolalvarez
Colaborador
 
Fecha de Ingreso: julio-2008
Ubicación: Caracas
Mensajes: 12.120
Antigüedad: 15 años, 8 meses
Puntos: 1532
Respuesta: problemas con httprequest

no te carga nada porque no veo que definas la función que espera la respuesta, es decir el onreadystatechange, consulta un tutorial de ajax
__________________
¡Por favor!: usa el highlight para mostrar código
El que busca, encuentra...
  #3 (permalink)  
Antiguo 07/07/2011, 07:49
Avatar de Melecio  
Fecha de Ingreso: julio-2011
Ubicación: Coahuila
Mensajes: 320
Antigüedad: 12 años, 9 meses
Puntos: 8
Respuesta: problemas con httprequest

ok creo que seria asi remplaza tu funcion por esta

y en body llama la funcion loadajax()

--------------------

<script type="text/javascript">
function getXMLHTTPRequest() {
try {
req = new XMLHttpRequest();
} catch(err1) {
try {
req = new ActiveXObject("Msxml2.XMLHTTP");
} catch (err2) {
try {
req = new ActiveXObject("Microsoft.XMLHTTP");
} catch (err3) {
req = false;
}
}
}
return req;
}

var http = getXMLHTTPRequest();

function loadajax() {
var myurl = 'notices.xml';
myRand = parseInt(Math.random()*999999999999999);
var modurl = myurl+"?rand="+myRand;
http.open("GET", modurl, true);
http.onreadystatechange = useHttpResponse;
http.send(null);
}

function useHttpResponse() {
if (http.readyState == 4) {

if(http.status == 200) {



var x = http.responseXML.getElementsByTagName("RCONTENT");
var y = http.responseXML.getElementsByTagName("TITTLE");
var z = http.responseXML.getElementsByTagName("CONTENT");

var rcon = "";
var cont = "";
var tit = "";

for (a=0;a<y.length;a++){
tit=tit + y[a].childNodes[0].nodeValue + "<br />" ;
document.getElementById("tittle").innerHTML=tit;
}



for (i=0;i<x.length;i++){
rcon=rcon + x[i].childNodes[0].nodeValue + "<br />" ;
document.getElementById("noticias").innerHTML=rcon ;
}



for (b=0;b<z.length;b++){
cont=cont + z[b].childNodes[0].nodeValue + "<br />" ;
document.getElementById("content").innerHTML=cont;
}





}
} else {

}
}



</script>
  #4 (permalink)  
Antiguo 07/07/2011, 13:28
 
Fecha de Ingreso: mayo-2011
Mensajes: 42
Antigüedad: 12 años, 11 meses
Puntos: 0
Respuesta: problemas con httprequest

estoy siguiendo los pasos del tutorial de w3schools de ajax por ahora tengo esto y cuando abro la pagina me salta el error de "no se ha podido cargar el archivo" y los valores de readyState y status son 4 y 0 respectivamente, según el tutorial el status solo puede ser 200 = "OK" o 404 el famoso page not found, pero a mi me da 0 por que sera? el código es este:
Código:
var xmlhttp;
//------CREAMOS OBJETO------//
	if(window.XMLHttpRequest){
		xmlhttp = new XMLHttpRequest;
	}
	else{
		xmlhttp = new ActiveObject("Microsoft.XMLHTTP");
	}
//------CREAMOS OBJETO------//

		xmlhttp.open("GET","notices.xml", true);
		xmlhttp.send();
		xmlhttp.onreadystatechange=getXMLData();

	function getXMLData(){
	if (xmlhttp.readyState = 4 && xmlhttp.status == 200){
		
		xmlDoc = xmlhttp.responseXML;
		txt = "";
		x = xmlDoc.getElementsByTagName("RCONTENT");
		
			for (i=0 ; i< x.length ; i++){
				txt = txt + x[i].childNodes[0].nodeValue + "<br/>";
			}
		
		document.getElementById("noticias").innerHTML = txt;
		}

	else{
		alert('no se pudo cargar el archivo');
		document.write(xmlhttp.readyState + "<br/>");
		document.write(xmlhttp.status);
	}
	}
  #5 (permalink)  
Antiguo 07/07/2011, 14:07
Avatar de Melecio  
Fecha de Ingreso: julio-2011
Ubicación: Coahuila
Mensajes: 320
Antigüedad: 12 años, 9 meses
Puntos: 8
Respuesta: problemas con httprequest

pues le movi un poco pero en ie explorer no me funciono en firefox si jalo le voy a intentar aber si sale...


--------------------
var xmlhttp;
var xmlhttp;
//------CREAMOS OBJETO------//
if(window.XMLHttpRequest){
xmlhttp = new XMLHttpRequest;
}
else{
xmlhttp = new ActiveObject("Microsoft.XMLHTTP");
}


//------CREAMOS OBJETO------//

xmlhttp.open("GET","notices.xml", true);

xmlhttp.onreadystatechange = getXMLData;
xmlhttp.send(null);


function getXMLData(){

if (xmlhttp.readyState = 4 && xmlhttp.status == 200){

xmlDoc = xmlhttp.responseXML;
txt = "";
x = xmlDoc.getElementsByTagName("RCONTENT");

for (i=0 ; i< x.length ; i++){
txt = txt + x[i].childNodes[0].nodeValue + "<br/>";
}

document.getElementById("noticias").innerHTML = txt;
}else{
alert('no se pudo cargar el archivo');
document.write(xmlhttp.readyState + "<br/>");
document.write(xmlhttp.status);
}
}
  #6 (permalink)  
Antiguo 07/07/2011, 14:17
 
Fecha de Ingreso: mayo-2011
Mensajes: 42
Antigüedad: 12 años, 11 meses
Puntos: 0
Respuesta: problemas con httprequest

A mi no me funciona ni en IE ni en firefox ni en chrome, una pregunta, esto deberia estar en un servidor o abriendolo desde el disco deberia funcionar ?
  #7 (permalink)  
Antiguo 07/07/2011, 14:26
Avatar de Melecio  
Fecha de Ingreso: julio-2011
Ubicación: Coahuila
Mensajes: 320
Antigüedad: 12 años, 9 meses
Puntos: 8
Respuesta: problemas con httprequest

Cita:
Iniciado por facuferrari Ver Mensaje
A mi no me funciona ni en IE ni en firefox ni en chrome, una pregunta, esto deberia estar en un servidor o abriendolo desde el disco deberia funcionar ?


haora intenta con este a mi si me funciono en cualquier navegador

<script type="text/javascript">
function getXMLHTTPRequest() {
try {
req = new XMLHttpRequest();
} catch(err1) {
try {
req = new ActiveXObject("Msxml2.XMLHTTP");
} catch (err2) {
try {
req = new ActiveXObject("Microsoft.XMLHTTP");
} catch (err3) {
req = false;
}
}
}
return req;
}

var xmlhttp= getXMLHTTPRequest();

//------CREAMOS OBJETO------//

xmlhttp.open("GET","notices.xml", true);

var ww = xmlhttp.onreadystatechange = getXMLData;
xmlhttp.send(null);

function getXMLData(){

var ww= xmlhttp.readyState ;
var w1= xmlhttp.status ;



if (xmlhttp.readyState == 4) {

if (xmlhttp.status == 200) {

xmlDoc = xmlhttp.responseXML;
txt = "";
x = xmlDoc.getElementsByTagName("RCONTENT");



for (i=0 ; i< x.length ; i++){
txt = txt + x[i].childNodes[0].nodeValue + "<br/>";
document.getElementById("noticias").innerHTML = txt;
}

}
}
}

</script>
  #8 (permalink)  
Antiguo 07/07/2011, 14:29
Avatar de Melecio  
Fecha de Ingreso: julio-2011
Ubicación: Coahuila
Mensajes: 320
Antigüedad: 12 años, 9 meses
Puntos: 8
Respuesta: problemas con httprequest

lo cale como tu lo haces solo dandole clik al html en la carpeta sin usar wampserver
y no me funciono


la razon es esa sin un emulador como wamp el estatus que es 4 si lo hace pero esl 200 no se que da en cero
y pues no inicializa y por lotanto no hace el proceso

pero como yo estoy utilizando wapserver como localhost si funciona a la perfeccion

suerte!!

Última edición por Melecio; 07/07/2011 a las 14:34 Razón: razon
  #9 (permalink)  
Antiguo 07/07/2011, 16:23
 
Fecha de Ingreso: mayo-2011
Mensajes: 42
Antigüedad: 12 años, 11 meses
Puntos: 0
Respuesta: problemas con httprequest

usando wamp server y ese script, aun así no me funciona, gracias de todas formas ! pongo lo que tengo hasta ahora para ver si le encuentras algun error.
Código:
<!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>home page | notice</title>
<link href="notice_style.css" rel="stylesheet" type="text/css" />

<script type="text/javascript">
function getXMLHTTPRequest() {
try {
req = new XMLHttpRequest();
} catch(err1) {
try {
req = new ActiveXObject("Msxml2.XMLHTTP");
} catch (err2) {
try {
req = new ActiveXObject("Microsoft.XMLHTTP");
} catch (err3) {
req = false;
}
}
}
return req;
}

var xmlhttp= getXMLHTTPRequest();

//------CREAMOS OBJETO------//

xmlhttp.open("GET","notices.xml", true);

var ww = xmlhttp.onreadystatechange = getXMLData;
xmlhttp.send(null);

function getXMLData(){

var ww= xmlhttp.readyState ;
var w1= xmlhttp.status ;



if (xmlhttp.readyState == 4) {

if (xmlhttp.status == 200) {

xmlDoc = xmlhttp.responseXML;
txt = "";
x = xmlDoc.getElementsByTagName("RCONTENT");



for (i=0 ; i< x.length ; i++){
txt = txt + x[i].childNodes[0].nodeValue + "<br/>";
document.getElementById("noticias").innerHTML = txt;
}

}
}
}

</script>
</head>

<body onload='getXMLHTTPRequest()'>

<div id='content'>

<div id='tittle'></div>

<div id='noticias'>

</div>

</div>
</body>
</html>
y el xml:
Código:
<?xml version="1.0" encoding="iso-8859-1"?>

<TITTLE>noticia 1</TITTLE>
<RCONTENT>Contenido reducido</RCONTENT>
<CONTENT>Contenido completo</CONTENT>


<TITTLE>noticia 2</TITTLE>
<RCONTENT>Contenido reducido</RCONTENT>
<CONTENT>Contenido completo</CONTENT>
  #10 (permalink)  
Antiguo 08/07/2011, 07:07
Avatar de Melecio  
Fecha de Ingreso: julio-2011
Ubicación: Coahuila
Mensajes: 320
Antigüedad: 12 años, 9 meses
Puntos: 8
Respuesta: problemas con httprequest

Cita:
Iniciado por facuferrari Ver Mensaje
usando wamp server y ese script, aun así no me funciona, gracias de todas formas ! pongo lo que tengo hasta ahora para ver si le encuentras algun error.
Código:
<!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>home page | notice</title>
<link href="notice_style.css" rel="stylesheet" type="text/css" />

<script type="text/javascript">
function getXMLHTTPRequest() {
try {
req = new XMLHttpRequest();
} catch(err1) {
try {
req = new ActiveXObject("Msxml2.XMLHTTP");
} catch (err2) {
try {
req = new ActiveXObject("Microsoft.XMLHTTP");
} catch (err3) {
req = false;
}
}
}
return req;
}

var xmlhttp= getXMLHTTPRequest();

//------CREAMOS OBJETO------//

xmlhttp.open("GET","notices.xml", true);

var ww = xmlhttp.onreadystatechange = getXMLData;
xmlhttp.send(null);

function getXMLData(){

var ww= xmlhttp.readyState ;
var w1= xmlhttp.status ;



if (xmlhttp.readyState == 4) {

if (xmlhttp.status == 200) {

xmlDoc = xmlhttp.responseXML;
txt = "";
x = xmlDoc.getElementsByTagName("RCONTENT");



for (i=0 ; i< x.length ; i++){
txt = txt + x[i].childNodes[0].nodeValue + "<br/>";
document.getElementById("noticias").innerHTML = txt;
}

}
}
}

</script>
</head>

<body onload='getXMLHTTPRequest()'>

<div id='content'>

<div id='tittle'></div>

<div id='noticias'>

</div>

</div>
</body>
</html>
y el xml:
Código:
<?xml version="1.0" encoding="iso-8859-1"?>

<TITTLE>noticia 1</TITTLE>
<RCONTENT>Contenido reducido</RCONTENT>
<CONTENT>Contenido completo</CONTENT>


<TITTLE>noticia 2</TITTLE>
<RCONTENT>Contenido reducido</RCONTENT>
<CONTENT>Contenido completo</CONTENT>



ok creo que es por tu xml le falta esto


<?xml version="1.0" encoding="iso-8859-1"?>

<nodo>

<TITTLE>noticia 1</TITTLE>
<RCONTENT>Contenido reducido</RCONTENT>
<CONTENT>Contenido completo</CONTENT>


<TITTLE>noticia 2</TITTLE>
<RCONTENT>Contenido reducido</RCONTENT>
<CONTENT>Contenido completo</CONTENT>

</nodo>

Etiquetas: ajax, contenido, httprequest
Atención: Estás leyendo un tema que no tiene actividad desde hace más de 6 MESES, te recomendamos abrir un Nuevo tema en lugar de responder al actual.
Respuesta




La zona horaria es GMT -6. Ahora son las 12:37.