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

Problema AJAX ResponseXML

Estas en el tema de Problema AJAX ResponseXML en el foro de Frameworks JS en Foros del Web. Hola a todos necesito ayuda. Cuando quiero recuperar el texto desde ajax con responseText anda perfecto. Cuando recupero con responseXML no funciona. Es como que ...
  #1 (permalink)  
Antiguo 18/07/2011, 10:59
Avatar de javi10823  
Fecha de Ingreso: agosto-2008
Ubicación: Madrid, Spain, Spain
Mensajes: 17
Antigüedad: 15 años, 8 meses
Puntos: 2
Problema AJAX ResponseXML

Hola a todos necesito ayuda.
Cuando quiero recuperar el texto desde ajax con responseText anda perfecto.
Cuando recupero con responseXML no funciona.
Es como que me devuelve un doc xml pero con error.

Quiero acceder a los elementos y son nulos.

copio el codigo de la jsp:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Informacion sobre libros</title>
<script src="./jquery/jquery-1.6.2.min.js" type="text/javascript"></script>
<script src="./AJAX/ajax.js" type="text/javascript"></script>
<script type="text/javascript">

var value=null;
var ajax=null;

function buscarComentario()

{

var lista = document.getElementById("titulo");

var tit = lista.options[lista.selectedIndex].value;

if (tit!=null && tit.length!=0)

{



if (window.ActiveXOnject)

{
// alert("crea ajax a");
ajax= new ActiveXObject("Microsoft.XMLHTTP");
}
else if ((window.XMLHttpRequest) || (typeof XMLHttpRequest)!=undefined)
{

//alert("crea ajax b");
ajax = new XMLHttpRequest();
}
else
{
// alert("no lo crea");
ajax=null;

}

ajax.onreadystatechange = function() {

if (ajax.readyState==4) {


funcionCallback();


}
};



// Enviamos la peticion
ajax.open( "GET", "Servlet_ComentarioXML?tit="+tit, true );
ajax.send("");


}



}


function funcionCallback()
{

//alert("funcion callback");

// Comprobamos si la peticion se ha completado (estado 4)
if( ajax.readyState == 4 )
{


// Comprobamos si la respuesta ha sido correcta (resultado HTTP 200)
if( ajax.status == 200 )
{



value=ajax.responseXML;

alert(value);

var elemento = value.getElementsByTagName("libro").item(0);

alert(elemento.childNodes.item(0).nodeValue);



}
}
}


</script>
</head>
<body bgcolor="#E6E6E6">
<center>
<table cellspacing="30">
<tr>
<td><img src="img/libros.gif" width="100" /></td>
<td><h2>Información sobre libros</h2></td>
</tr>
</table>
<table width="30%">
<tr>
<td><b>Elija titulo de Libro</b></td>
</tr>
<tr>
<td><b><SELECT id="titulo" onchange="buscarComentario()">
<option>--Seleccione titulo</option>
<option value="0">Programación con c#</option>
<option value="1">ASP.NET</option>
<option value="2">AJAX en un día</option>
<option value="3">J2EE</option>
</SELECT></b></td>
</tr>
<tr>
<td>
<br/><br/>
<div id="comentario">

</div></td>
</tr>
</table>

</center>
</body>
</html>




y este es el codigo del servlet:


package com.practica2;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
* Servlet implementation class Servlet_ComentarioXML
*/
public class Servlet_ComentarioXML extends HttpServlet {
private static final long serialVersionUID = 1L;

/**
* @see HttpServlet#HttpServlet()
*/
public Servlet_ComentarioXML() {
super();
// TODO Auto-generated constructor stub
}

/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub


response.setContentType("text/xml; charset=UTF-8");
response.setHeader("Cache-Control", "no-cache");

PrintWriter out = response.getWriter();

//array de descripciones
String [] comentarios = {"conocimiento de POO","Puede construir f&aacute;cilmente aplicaciones web",
"Aprender&aacute; r&aacute;pidamente los ppales trucos de Ajax","Introduce las ppales tecnologias de la plataforma"};

//precios
String [] precios={"23.5","31.4","32.0","27.5"};

int sel;

//tit es el parametro

sel = Integer.parseInt(request.getParameter("tit"));

String textoXML = " <?xml version=\"1.0\" encoding=\"iso-8859-1\"?"+"> ";
textoXML+= "<libro>";
textoXML+= "<comentario>"+comentarios[sel]+"</comentario>";
textoXML+= "<precio>"+precios[sel]+"</precio>";
textoXML+= "</libro>";



out.println(textoXML);
out.close();


}

/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
}

}
  #2 (permalink)  
Antiguo 18/07/2011, 11:06
Avatar de caricatos
Moderador
 
Fecha de Ingreso: abril-2002
Ubicación: Torremolinos (Málaga)
Mensajes: 19.607
Antigüedad: 22 años
Puntos: 1284
Respuesta: Problema AJAX ResponseXML

Hola:

No sé cuántos errores tendrá, pero evidentemente el xml está mal formado... el meta del principio debe terminar en "/>" (cerrando el tag en sí mismo)... posiblemente el tag script tengas que ponerlo como comentario... supongo que algún validador xml te ayudará más que yo... hay algunos online.

Saludos
__________________
Por favor:
No hagan preguntas de temas de foros en mensajes privados... no las respondo
  #3 (permalink)  
Antiguo 18/07/2011, 12:14
Avatar de javi10823  
Fecha de Ingreso: agosto-2008
Ubicación: Madrid, Spain, Spain
Mensajes: 17
Antigüedad: 15 años, 8 meses
Puntos: 2
Respuesta: Problema AJAX ResponseXML

hola, intenté cambiarlo por esto:

<?xml version="1.0" ?><libro><comentario>Aprendera rapidamente los ppales trucos de Ajax</comentario><precio>32.0</precio></libro>

que no creo que tenga errores...

y sigue sin funcionarme.

:(

saludos

Etiquetas: ajax, jquery, post, select
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 07:52.