Ver Mensaje Individual
  #1 (permalink)  
Antiguo 14/07/2012, 20:34
link01
 
Fecha de Ingreso: octubre-2011
Mensajes: 58
Antigüedad: 12 años, 7 meses
Puntos: 0
Responder petición al objeto XMLHttpRequest con AJAX

Estoy aprendiendo ajax.

Mi objetivo ahora mismo es crear un formulario y al presionar el botón enviar, me salgan los datos enviados en un div.

Este es el codigo html con el formulario creado:
Código HTML:
Ver original
  1. <h1> Formulario de prueba </h1>
  2.  
  3.     <form method="post" action="">
  4.         <table border="1">
  5.             <tr>
  6.                 <td> Nombre </td>
  7.                 <td> <input type="text" name="txtNombre" />
  8.             </tr>
  9.             <tr>
  10.                 <td> Apellido </td>
  11.                 <td> <input type="text" name="txtApellido" />
  12.             </tr>
  13.             <tr>
  14.                 <td colspan="2">
  15.                     <button type="button"> Enviar </button>
  16.                 </td>
  17.             </tr>
  18.         </table>
  19.     </form>
  20.  
  21.     <div id="datosEnviados"></div>

El codigo javascript/ajax
Código Javascript:
Ver original
  1. <script type="text/javascript">
  2.         var peticion_http = null;
  3.         var READY_STATE_COMPLETE = 4;
  4.  
  5.         function iniciar_xhr(){
  6.             if(window.XMLHttpRequest){
  7.                 return new XMLHttpRequest();
  8.             }
  9.         }
  10.  
  11.         window.onload = function(){
  12.             peticion_http = iniciar_xhr();
  13.             btnEnviar = document.getElementsByTagName("button")[0];
  14.             btnEnviar.onclick = enviarPeticion;
  15.         }
  16.  
  17.         function enviarPeticion(){
  18.             nombre = document.getElementsByName("txtNombre")[0].value;
  19.             apellido = document.getElementsByName("txtApellido")[0].value;
  20.  
  21.             query_string = "nombre=" + encodeURIComponent(nombre) +
  22.                            "&apellido=" + encodeURIComponent(apellido) +
  23.                            "&nocache=" +Math.random();
  24.  
  25.  
  26.             peticion_http.onreadystatechange = verDatosRecibidos;
  27.             peticion_http.open("POST","http://localhost/AJAX/formulario.php", true);
  28.             peticion_http.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
  29.             peticion_http.send(query_string);
  30.  
  31.         }
  32.  
  33.         function verDatosRecibidos(){
  34.             if(peticion_http.readyState == READY_STATE_COMPLETE){
  35.                 if(peticion_http.status == 200) {
  36.                     alert(peticion_http.responseText);
  37.                 }
  38.             }
  39.            
  40.         }
  41.     </script>

Por ahora eso funciona, pero el problema es que la propiedad responseText del objeto XMLHttpRequest me trae TODO el contenido del documento, todo el codigo de la pagina completa (que también incluye los datos enviados por post) pero yo solo quiero los datos que se enviaron ¿no hay alguna forma de filtrarlos sin tener que usar XML?
__________________
La libertad más difícil de conservar es la de equivocarse. - Morris Wes

Lo que faltaba en internet: http://binar10s.blogspot.com/