Foros del Web » Programando para Internet » Javascript »

tengo un problema muy interesante sobre ajax

Estas en el tema de tengo un problema muy interesante sobre ajax en el foro de Javascript en Foros del Web. tengo un problema muy interesante sobre ajax, esto funciona bien pero quiero dividir la forma de traer contenido, es decir uno traega <div id="dina"></div><!--aqui quiero ...
  #1 (permalink)  
Antiguo 25/06/2013, 18:58
Avatar de jor_0203  
Fecha de Ingreso: octubre-2011
Ubicación: mexico
Mensajes: 760
Antigüedad: 12 años, 6 meses
Puntos: 8
tengo un problema muy interesante sobre ajax

tengo un problema muy interesante sobre ajax, esto funciona bien pero quiero dividir la forma de traer contenido, es decir uno
traega <div id="dina"></div><!--aqui quiero traer de la pagina solo la variable $_GET['var']; -->
y en
<div id="apellidoDiv"></div> <!--aqui quiero traer de la pagina solo la variable $_GET['c2'];-->
mi pregunta esto se puede ya que se que con ajax te gala todo
pero quiero jalar todo en partes
es decir nombre de la variable var sea jalado en el div con nombre dina
y
apellido que es la variable c2 entre en el div apellidoDiv
pero no se si esto se puede

me gustaría saber su opinión


Código HTML:
Ver original
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  2. <html xmlns="http://www.w3.org/1999/xhtml">
  3. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  4. <title>Documento sin t&iacute;tulo</title>
  5. <script type="text/javascript">
  6. function requerir(){
  7.     try{
  8.     req=new XMLHttpRequest();
  9.     }catch(err1){
  10.         try{
  11.         req=new ActiveXObject("Microsoft.XMLHTTP");
  12.         }catch(err2){
  13.             try{
  14.             req=new ActiveXObject("Msxml2.XMLHTTP");
  15.             }catch(err3){
  16.             req= false;
  17.             }
  18.         }
  19.     }
  20. return req;
  21. }
  22. var peticion=requerir();
  23.  
  24. function llamarAjax(){
  25. var aleatorio=parseInt(Math.random()*999999999);
  26. var campo=document.form1.apellido.value;
  27. var campo2=document.form1.nombre.value;
  28. var url="codigo.php?var="+campo+"&r="+aleatorio+"&c2="+campo2;
  29. peticion.open("GET",url,true);
  30. peticion.onreadystatechange =respuestaAjax;
  31. peticion.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
  32. peticion.send(null);
  33. }
  34.  
  35. function respuestaAjax(){
  36.     if(peticion.readyState==4)
  37.     {
  38.         if(peticion.status==200)
  39.         {
  40.         //alert(peticion.responseText);
  41.         document.getElementById("dina").innerHTML=peticion.responseText;
  42.         }
  43.         else
  44.         {
  45.         alert("ha ocurrido un error"+peticion.statusText);
  46.         }
  47.     }
  48.     else
  49.     {
  50.     document.getElementById("dina").innerHTML='<img src="carga.gif" />';
  51.     }
  52. }
  53.  
  54.  
  55. </head>
  56. <div id="dina"></div>
  57. <form action="" method="post" enctype="application/x-www-form-urlencoded" name="form1" id="form1">
  58. <input type="text" name="nombre" id="nombre" />
  59. <div id="dina"></div><!--aqui quiero traer de la pagina solo la variable  $_GET['var']; -->
  60. <input type="text" name="apellido" id="apellido" />
  61. <div id="apellidoDiv"></div> <!--aqui quiero traer de la pagina solo la variable  $_GET['c2'];-->
  62.  
  63. <input name="" type="button" onclick="llamarAjax()" />
  64. </form>
  65. </body>
  66. </html>




Código PHP:
Ver original
  1. <?php
  2. /*la pagina se llama codigo*/
  3. echo $_GET['var'];
  4. echo $_GET['c2'];
  5.  
  6. ?>

Última edición por jor_0203; 25/06/2013 a las 19:09
  #2 (permalink)  
Antiguo 25/06/2013, 22:52
Avatar de Franz1628  
Fecha de Ingreso: marzo-2007
Ubicación: Lima - Perú
Mensajes: 145
Antigüedad: 17 años, 1 mes
Puntos: 26
Respuesta: tengo un problema muy interesante sobre ajax

Lo que puedes hacer es traerlo como xml

Código PHP:
Ver original
  1. <?php
  2.     header('Content-Type: text/xml');
  3.  
  4.     echo "<?xml version=\"1.0\"?><var>".$_GET['var']."</var><c2>".$_GET['c2']."</c2>";
  5. ?>

DEbes modificar tu funcion respuestaAjax algo asi

Código Javascript:
Ver original
  1. function respuestaAjax(){
  2.     if(peticion.readyState==4)
  3.     {
  4.         if(peticion.status==200)
  5.         {
  6.         //alert(peticion.responseText);
  7.        
  8.      
  9.         var dina =  peticion.responseXML.getElementsByTagName("var")[0];
  10.         var apellidoDiv=  peticion.responseXML.getElementsByTagName("c2")[0];
  11.  
  12.            document.getElementById("dina").innerHTML=dina.childNodes[0].nodeValue;
  13.            document.getElementById("apellidoDiv").innerHTML=apellidoDiv.childNodes[0].nodeValue;
  14.         }
  15.         else
  16.         {
  17.         alert("ha ocurrido un error"+peticion.statusText);
  18.         }
  19.     }
  20.     else
  21.     {
  22.     document.getElementById("dina").innerHTML='<img src="carga.gif" />';
  23.     }
  24. }

Saludos
__________________
En mi Blog puedes ver articulos javascript y más...
@Franz1628
  #3 (permalink)  
Antiguo 26/06/2013, 14:35
Avatar de jor_0203  
Fecha de Ingreso: octubre-2011
Ubicación: mexico
Mensajes: 760
Antigüedad: 12 años, 6 meses
Puntos: 8
Respuesta: tengo un problema muy interesante sobre ajax

Cita:
Iniciado por Franz1628 Ver Mensaje
Lo que puedes hacer es traerlo como xml

Código PHP:
Ver original
  1. <?php
  2.     header('Content-Type: text/xml');
  3.  
  4.     echo "<?xml version=\"1.0\"?><var>".$_GET['var']."</var><c2>".$_GET['c2']."</c2>";
  5. ?>

DEbes modificar tu funcion respuestaAjax algo asi

Código Javascript:
Ver original
  1. function respuestaAjax(){
  2.     if(peticion.readyState==4)
  3.     {
  4.         if(peticion.status==200)
  5.         {
  6.         //alert(peticion.responseText);
  7.        
  8.      
  9.         var dina =  peticion.responseXML.getElementsByTagName("var")[0];
  10.         var apellidoDiv=  peticion.responseXML.getElementsByTagName("c2")[0];
  11.  
  12.            document.getElementById("dina").innerHTML=dina.childNodes[0].nodeValue;
  13.            document.getElementById("apellidoDiv").innerHTML=apellidoDiv.childNodes[0].nodeValue;
  14.         }
  15.         else
  16.         {
  17.         alert("ha ocurrido un error"+peticion.statusText);
  18.         }
  19.     }
  20.     else
  21.     {
  22.     document.getElementById("dina").innerHTML='<img src="carga.gif" />';
  23.     }
  24. }

Saludos
entonces me recomiendas
que le meta xml
y con eso lo puedo hacer
es correcto
  #4 (permalink)  
Antiguo 26/06/2013, 14:48
Avatar de jor_0203  
Fecha de Ingreso: octubre-2011
Ubicación: mexico
Mensajes: 760
Antigüedad: 12 años, 6 meses
Puntos: 8
Respuesta: tengo un problema muy interesante sobre ajax

lo puse asi y no me sale , la pagina es a1,php
Código PHP:
Ver original
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  2. <html xmlns="http://www.w3.org/1999/xhtml">
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  5. <title>Documento sin t&iacute;tulo</title>
  6. <script type="text/javascript">
  7. function requerir(){
  8.     try{
  9.     req=new XMLHttpRequest();
  10.     }catch(err1){
  11.         try{
  12.         req=new ActiveXObject("Microsoft.XMLHTTP");
  13.         }catch(err2){
  14.             try{
  15.             req=new ActiveXObject("Msxml2.XMLHTTP");
  16.             }catch(err3){
  17.             req= false;
  18.             }
  19.         }
  20.     }
  21. return req;
  22. }
  23. var peticion=requerir();
  24.  
  25. function llamarAjax(){
  26. var aleatorio=parseInt(Math.random()*999999999);
  27. var campo=document.form1.apellido.value;
  28. var campo2=document.form1.nombre.value;
  29. var url="codigo.xml?var="+campo+"&r="+aleatorio+"&c2="+campo2;
  30. peticion.open("GET",url,true);
  31. peticion.onreadystatechange =respuestaAjax;
  32. peticion.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
  33. peticion.send(null);
  34. }
  35.  
  36. function respuestaAjax(){
  37.     if(peticion.readyState==4)
  38.     {
  39.         if(peticion.status==200)
  40.         {
  41.         //alert(peticion.responseText);
  42.        
  43.      
  44.         var dina =  peticion.responseXML.getElementsByTagName("var")[0];
  45.         var apellidoDiv=  peticion.responseXML.getElementsByTagName("c2")[0];
  46.  
  47.            document.getElementById("dina").innerHTML=dina.childNodes[0].nodeValue;
  48.            document.getElementById("apellidoDiv").innerHTML=apellidoDiv.childNodes[0].nodeValue;
  49.         }
  50.         else
  51.         {
  52.         alert("ha ocurrido un error"+peticion.statusText);
  53.         }
  54.     }
  55.     else
  56.     {
  57.     document.getElementById("dina").innerHTML='<img src="carga.gif" />';
  58.     }
  59. }
  60.  
  61.  
  62. </script>
  63. </head>
  64. <body>
  65. <div id="dina"></div>
  66. <form action="" method="post" enctype="application/x-www-form-urlencoded" name="form1" id="form1">
  67. <label>
  68. <input type="text" name="nombre" id="nombre" />
  69. <div id="dina"></div><!--aqui quiero traer de la pagina solo la variable  $_GET['var']; -->
  70. </label>
  71. <label>
  72. <input type="text" name="apellido" id="apellido" />
  73. <div id="apellidoDiv"></div> <!--aqui quiero traer de la pagina solo la variable  $_GET['c2'];-->
  74. </label>
  75.  
  76. <input name="" type="button" onclick="llamarAjax()" />
  77. </form>
  78. </body>
  79. </html>

la pagina es codigo.xml no se si asi esta bien
gracias

Código XML:
Ver original
  1. <?php
  2.  
  3.   header('Content-Type: text/xml');
  4.    echo "<?xml version=\"1.0\"?><var>".$_GET['var']."</var><c2>".$_GET['c2']."</c2>";
  5.    
  6. ?>
  #5 (permalink)  
Antiguo 26/06/2013, 14:54
Avatar de Franz1628  
Fecha de Ingreso: marzo-2007
Ubicación: Lima - Perú
Mensajes: 145
Antigüedad: 17 años, 1 mes
Puntos: 26
Respuesta: tengo un problema muy interesante sobre ajax

no
que tu archivo php sea asi

Código PHP:
Ver original
  1. <?php
  2.  
  3.    header('Content-Type: text/xml');
  4.     echo "<?xml version=\"1.0\"?><var>".$_GET['var']."</var><c2>".$_GET['c2']."</c2>";
  5.    
  6. ?>
__________________
En mi Blog puedes ver articulos javascript y más...
@Franz1628
  #6 (permalink)  
Antiguo 26/06/2013, 15:11
Avatar de jor_0203  
Fecha de Ingreso: octubre-2011
Ubicación: mexico
Mensajes: 760
Antigüedad: 12 años, 6 meses
Puntos: 8
Respuesta: tengo un problema muy interesante sobre ajax

lo puse a si y no me sale
esta es la pagina a1.php

Código PHP:
Ver original
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  2. <html xmlns="http://www.w3.org/1999/xhtml">
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  5. <title>Documento sin t&iacute;tulo</title>
  6. <script type="text/javascript">
  7. function requerir(){
  8.     try{
  9.     req=new XMLHttpRequest();
  10.     }catch(err1){
  11.         try{
  12.         req=new ActiveXObject("Microsoft.XMLHTTP");
  13.         }catch(err2){
  14.             try{
  15.             req=new ActiveXObject("Msxml2.XMLHTTP");
  16.             }catch(err3){
  17.             req= false;
  18.             }
  19.         }
  20.     }
  21. return req;
  22. }
  23. var peticion=requerir();
  24.  
  25. function llamarAjax(){
  26. var aleatorio=parseInt(Math.random()*999999999);
  27. var campo=document.form1.apellido.value;
  28. var campo2=document.form1.nombre.value;
  29. var url="codigo.php?var="+campo+"&r="+aleatorio+"&c2="+campo2;
  30. peticion.open("GET",url,true);
  31. peticion.onreadystatechange =respuestaAjax;
  32. peticion.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
  33. peticion.send(null);
  34. }
  35.  
  36. function respuestaAjax(){
  37.     if(peticion.readyState==4)
  38.     {
  39.         if(peticion.status==200)
  40.         {
  41.         //alert(peticion.responseText);
  42.        
  43.      
  44.         var dina =  peticion.responseXML.getElementsByTagName("var")[0];
  45.         var apellidoDiv=  peticion.responseXML.getElementsByTagName("c2")[0];
  46.  
  47.            document.getElementById("dina").innerHTML=dina.childNodes[0].nodeValue;
  48.            document.getElementById("apellidoDiv").innerHTML=apellidoDiv.childNodes[0].nodeValue;
  49.         }
  50.         else
  51.         {
  52.         alert("ha ocurrido un error"+peticion.statusText);
  53.         }
  54.     }
  55.     else
  56.     {
  57.     document.getElementById("dina").innerHTML='<img src="carga.gif" />';
  58.     }
  59. }
  60.  
  61.  
  62. </script>
  63. </head>
  64. <body>
  65. <div id="dina"></div>
  66. <form action="" method="post" enctype="application/x-www-form-urlencoded" name="form1" id="form1">
  67. <label>
  68. <input type="text" name="nombre" id="nombre" />
  69. <div id="dina"></div><!--aqui quiero traer de la pagina solo la variable  $_GET['var']; -->
  70. </label>
  71. <label>
  72. <input type="text" name="apellido" id="apellido" />
  73. <div id="apellidoDiv"></div> <!--aqui quiero traer de la pagina solo la variable  $_GET['c2'];-->
  74. </label>
  75.  
  76. <input name="" type="button" onclick="llamarAjax()" />
  77. </form>
  78. </body>
  79.  
  80. </html>

y esta es la pagina codigo.php
Código HTML:
Ver original
  1. <?php
  2.  
  3.   header('Content-Type: text/xml');
  4.    echo "<?xml version=\"1.0\"?><var>".$_GET['var']."</var><c2>".$_GET['c2']."</c2>";
  5.  
  6. ?>

lo peor es que solo sale el gif animado por el error que tiene
como le puedo hacer
  #7 (permalink)  
Antiguo 26/06/2013, 15:29
Avatar de Franz1628  
Fecha de Ingreso: marzo-2007
Ubicación: Lima - Perú
Mensajes: 145
Antigüedad: 17 años, 1 mes
Puntos: 26
Respuesta: tengo un problema muy interesante sobre ajax

cambia estas lineas a estas

Código Javascript:
Ver original
  1. document.getElementById("dina").innerHTML=dina.nodeValue;
  2. document.getElementById("apellidoDiv").innerHTML=apellidoDiv.nodeValue;
__________________
En mi Blog puedes ver articulos javascript y más...
@Franz1628
  #8 (permalink)  
Antiguo 26/06/2013, 16:01
Avatar de jor_0203  
Fecha de Ingreso: octubre-2011
Ubicación: mexico
Mensajes: 760
Antigüedad: 12 años, 6 meses
Puntos: 8
Respuesta: tengo un problema muy interesante sobre ajax

Cita:
Iniciado por Franz1628 Ver Mensaje
cambia estas lineas a estas

Código Javascript:
Ver original
  1. document.getElementById("dina").innerHTML=dina.nodeValue;
  2. document.getElementById("apellidoDiv").innerHTML=apellidoDiv.nodeValue;
ya las cambie y no sale
  #9 (permalink)  
Antiguo 26/06/2013, 16:20
Avatar de Reedyseth  
Fecha de Ingreso: enero-2009
Ubicación: Chihuahua, México
Mensajes: 419
Antigüedad: 15 años, 3 meses
Puntos: 36
Respuesta: tengo un problema muy interesante sobre ajax

Si estas aprendiendo a manejar puro Ajax con XMLHttpRequest vas a aprender y batallar a la vez. Pero si lo que quieres hacer es trabajar con Ajax y obtener resultados con mucho gusto te muestro como hacerlo con jQuery.ajax()

Espero tu respuesta
__________________
Reedyseth
Te ayudo? No olvides dar un +
blog:http://behstant.com/blog
En el blog:Tutoriales de Desarrollo Web PHP, Javascript, BD y más.
  #10 (permalink)  
Antiguo 26/06/2013, 16:47
Avatar de jor_0203  
Fecha de Ingreso: octubre-2011
Ubicación: mexico
Mensajes: 760
Antigüedad: 12 años, 6 meses
Puntos: 8
Respuesta: tengo un problema muy interesante sobre ajax

Cita:
Iniciado por Reedyseth Ver Mensaje
Si estas aprendiendo a manejar puro Ajax con XMLHttpRequest vas a aprender y batallar a la vez. Pero si lo que quieres hacer es trabajar con Ajax y obtener resultados con mucho gusto te muestro como hacerlo con jQuery.ajax()

Espero tu respuesta
estoy tratando de aprender ajax
si manejo un poquito o casi nada de jquiery
pero si tu me enseñas jquery te agradeceré
aunque te soy franco tambien quiero ajax jijji
soy un poco necio ya que si muchos de mis compañeros pueden
yo debo poder
gracias
  #11 (permalink)  
Antiguo 26/06/2013, 16:49
Avatar de Franz1628  
Fecha de Ingreso: marzo-2007
Ubicación: Lima - Perú
Mensajes: 145
Antigüedad: 17 años, 1 mes
Puntos: 26
Respuesta: tengo un problema muy interesante sobre ajax

Los estas enviando como POST por eso las variales deben ser $_POST['var'] $_POST['c2']

Código PHP:
Ver original
  1. <?php
  2.  
  3.    header('Content-Type: text/xml');
  4.     echo "<?xml version=\"1.0\"?><var>".$_POST['var']."</var><c2>".$_POST['c2']."</c2>";
  5.  
  6. ?>
__________________
En mi Blog puedes ver articulos javascript y más...
@Franz1628
  #12 (permalink)  
Antiguo 26/06/2013, 16:49
Avatar de Panino5001
Me alejo de Omelas
 
Fecha de Ingreso: mayo-2004
Ubicación: -34.637167,-58.462984
Mensajes: 5.148
Antigüedad: 20 años
Puntos: 834
Respuesta: tengo un problema muy interesante sobre ajax

Asegurate de tener desactivada la directiva short_open_tag de php o, en caso de que no puedas desactivarla, escapá como te muestran acá: http://www.programacionweb.net/artic...iculo/?num=219
  #13 (permalink)  
Antiguo 26/06/2013, 17:04
Avatar de jor_0203  
Fecha de Ingreso: octubre-2011
Ubicación: mexico
Mensajes: 760
Antigüedad: 12 años, 6 meses
Puntos: 8
Respuesta: tengo un problema muy interesante sobre ajax

Cita:
Iniciado por Franz1628 Ver Mensaje
Los estas enviando como POST por eso las variales deben ser $_POST['var'] $_POST['c2']

Código PHP:
Ver original
  1. <?php
  2.  
  3.    header('Content-Type: text/xml');
  4.     echo "<?xml version=\"1.0\"?><var>".$_POST['var']."</var><c2>".$_POST['c2']."</c2>";
  5.  
  6. ?>
no las estoy mandando con get
con ajax

asi es como las mando
var url="codigo.php?var="+campo+"&r="+aleatorio+"&c2=" +campo2;
peticion.open("GET",url,true);
  #14 (permalink)  
Antiguo 26/06/2013, 17:52
Avatar de Franz1628  
Fecha de Ingreso: marzo-2007
Ubicación: Lima - Perú
Mensajes: 145
Antigüedad: 17 años, 1 mes
Puntos: 26
Respuesta: tengo un problema muy interesante sobre ajax

amigo mejor prueba con esto a ver

Código HTML:
Ver original
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  2. <html xmlns="http://www.w3.org/1999/xhtml">
  3. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  4. <title>Documento sin t&iacute;tulo</title>
  5. <script type="text/javascript">
  6. function requerir(){
  7.     try{
  8.     req=new XMLHttpRequest();
  9.     }catch(err1){
  10.         try{
  11.         req=new ActiveXObject("Microsoft.XMLHTTP");
  12.         }catch(err2){
  13.             try{
  14.             req=new ActiveXObject("Msxml2.XMLHTTP");
  15.             }catch(err3){
  16.             req= false;
  17.             }
  18.         }
  19.     }
  20. return req;
  21. }
  22. var peticion=requerir();
  23.  
  24. function llamarAjax(){
  25. var aleatorio=parseInt(Math.random()*999999999);
  26. var campo=document.form1.apellido.value;
  27. var campo2=document.form1.nombre.value;
  28. var url="codigo.php?var="+campo+"&r="+aleatorio+"&c2="+campo2;
  29. peticion.open("GET",url,true);
  30. peticion.onreadystatechange =respuestaAjax;
  31. peticion.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
  32. peticion.send(null);
  33. }
  34.  
  35. function respuestaAjax(){
  36.     if(peticion.readyState==4)
  37.     {
  38.         if(peticion.status==200)
  39.         {
  40.             var miPeticion  = JSON.parse(peticion.response);
  41.  
  42.            document.getElementById("dina").innerHTML=miPeticion.var;
  43.            document.getElementById("apellidoDiv").innerHTML=miPeticion.c2;
  44.         }
  45.         else
  46.         {
  47.         alert("ha ocurrido un error"+peticion.statusText);
  48.         }
  49.     }
  50.     else
  51.     {
  52.     document.getElementById("dina").innerHTML='<img src="carga.gif" />';
  53.     }
  54. }
  55.  
  56.  
  57. </head>
  58. <div id="dina"></div>
  59. <form action="" method="post" enctype="application/x-www-form-urlencoded" name="form1" id="form1">
  60. <input type="text" name="nombre" id="nombre" />
  61. <div id="dina"></div><!--aqui quiero traer de la pagina solo la variable  $_GET['var']; -->
  62. <input type="text" name="apellido" id="apellido" />
  63. <div id="apellidoDiv"></div> <!--aqui quiero traer de la pagina solo la variable  $_GET['c2'];-->
  64.  
  65. <input name="" type="button" onclick="llamarAjax()" />
  66. </form>
  67. </body>
  68. </html>



codigo.php

Código PHP:
Ver original
  1. <?php
  2.     $array['var'] = $_GET['var'];
  3.     $array['c2'] = $_GET['c2'];
  4.     echo json_encode($array);
  5. ?>
__________________
En mi Blog puedes ver articulos javascript y más...
@Franz1628
  #15 (permalink)  
Antiguo 26/06/2013, 23:28
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: tengo un problema muy interesante sobre ajax

Hola:

¿Te fijaste si el xml está bien formado?, es cuestión de abrirlo simplemente con el navegador, me suena que le falta la etiqueta que contenga todo...

<?php

header('Content-Type: text/xml');
echo "<?xml version=\"1.0\"?><respuesta><var>".$_POST['var']."</var><c2>".$_POST['c2']."</c2></respuesta>";

?>

Una referencia: Ajax, con X de XML.

Saludos
__________________
Por favor:
No hagan preguntas de temas de foros en mensajes privados... no las respondo
  #16 (permalink)  
Antiguo 27/06/2013, 08:44
Avatar de Reedyseth  
Fecha de Ingreso: enero-2009
Ubicación: Chihuahua, México
Mensajes: 419
Antigüedad: 15 años, 3 meses
Puntos: 36
Respuesta: tengo un problema muy interesante sobre ajax

Cita:
Iniciado por jor_0203 Ver Mensaje
estoy tratando de aprender ajax
si manejo un poquito o casi nada de jquiery
pero si tu me enseñas jquery te agradeceré
aunque te soy franco tambien quiero ajax jijji
soy un poco necio ya que si muchos de mis compañeros pueden
yo debo poder
gracias

Claro que se puede, de hecho no es tan complicado como parece, como he comentado anteriormente jQuery.ajax() es cuando te quieres mas enfocar en hacer un desarrollo rapido y mas eficiente, ya que el equipo de jQuery ha pasado horas depurando la funcion para que sea compatible ante cualquier situación, horas que tu puedes perder en vez de trabajar mas en tu proyeco.

No esta demás aprender realmente como funciona XMLHttpRequest, para que puedas entender realmente como funciona y para que puedas depurar mejor tu código.

Pero bueno, voy a revisar tú código y te comento.
__________________
Reedyseth
Te ayudo? No olvides dar un +
blog:http://behstant.com/blog
En el blog:Tutoriales de Desarrollo Web PHP, Javascript, BD y más.
  #17 (permalink)  
Antiguo 27/06/2013, 18:32
Avatar de jor_0203  
Fecha de Ingreso: octubre-2011
Ubicación: mexico
Mensajes: 760
Antigüedad: 12 años, 6 meses
Puntos: 8
Respuesta: tengo un problema muy interesante sobre ajax

Cita:
Iniciado por Franz1628 Ver Mensaje
amigo mejor prueba con esto a ver

Código HTML:
Ver original
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  2. <html xmlns="http://www.w3.org/1999/xhtml">
  3. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  4. <title>Documento sin t&iacute;tulo</title>
  5. <script type="text/javascript">
  6. function requerir(){
  7.     try{
  8.     req=new XMLHttpRequest();
  9.     }catch(err1){
  10.         try{
  11.         req=new ActiveXObject("Microsoft.XMLHTTP");
  12.         }catch(err2){
  13.             try{
  14.             req=new ActiveXObject("Msxml2.XMLHTTP");
  15.             }catch(err3){
  16.             req= false;
  17.             }
  18.         }
  19.     }
  20. return req;
  21. }
  22. var peticion=requerir();
  23.  
  24. function llamarAjax(){
  25. var aleatorio=parseInt(Math.random()*999999999);
  26. var campo=document.form1.apellido.value;
  27. var campo2=document.form1.nombre.value;
  28. var url="codigo.php?var="+campo+"&r="+aleatorio+"&c2="+campo2;
  29. peticion.open("GET",url,true);
  30. peticion.onreadystatechange =respuestaAjax;
  31. peticion.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
  32. peticion.send(null);
  33. }
  34.  
  35. function respuestaAjax(){
  36.     if(peticion.readyState==4)
  37.     {
  38.         if(peticion.status==200)
  39.         {
  40.             var miPeticion  = JSON.parse(peticion.response);
  41.  
  42.            document.getElementById("dina").innerHTML=miPeticion.var;
  43.            document.getElementById("apellidoDiv").innerHTML=miPeticion.c2;
  44.         }
  45.         else
  46.         {
  47.         alert("ha ocurrido un error"+peticion.statusText);
  48.         }
  49.     }
  50.     else
  51.     {
  52.     document.getElementById("dina").innerHTML='<img src="carga.gif" />';
  53.     }
  54. }
  55.  
  56.  
  57. </head>
  58. <div id="dina"></div>
  59. <form action="" method="post" enctype="application/x-www-form-urlencoded" name="form1" id="form1">
  60. <input type="text" name="nombre" id="nombre" />
  61. <div id="dina"></div><!--aqui quiero traer de la pagina solo la variable  $_GET['var']; -->
  62. <input type="text" name="apellido" id="apellido" />
  63. <div id="apellidoDiv"></div> <!--aqui quiero traer de la pagina solo la variable  $_GET['c2'];-->
  64.  
  65. <input name="" type="button" onclick="llamarAjax()" />
  66. </form>
  67. </body>
  68. </html>



codigo.php

Código PHP:
Ver original
  1. <?php
  2.     $array['var'] = $_GET['var'];
  3.     $array['c2'] = $_GET['c2'];
  4.     echo json_encode($array);
  5. ?>
que hermoso es lo que me enseñaste
mil gracias y que Dios te bendiga
realmente eres un master
  #18 (permalink)  
Antiguo 28/06/2013, 19:57
Avatar de jor_0203  
Fecha de Ingreso: octubre-2011
Ubicación: mexico
Mensajes: 760
Antigüedad: 12 años, 6 meses
Puntos: 8
Respuesta: tengo un problema muy interesante sobre ajax

Cita:
Iniciado por caricatos Ver Mensaje
Hola:

¿Te fijaste si el xml está bien formado?, es cuestión de abrirlo simplemente con el navegador, me suena que le falta la etiqueta que contenga todo...

<?php

header('Content-Type: text/xml');
echo "<?xml version=\"1.0\"?><respuesta><var>".$_POST['var']."</var><c2>".$_POST['c2']."</c2></respuesta>";

?>

Una referencia: Ajax, con X de XML.

Saludos

HERMANO ERA LO QUE DIJISTE GENIAL


ESTO QUEDO ASI



Código PHP:
Ver original
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  2. <html xmlns="http://www.w3.org/1999/xhtml">
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  5. <title>Documento sin t&iacute;tulo</title>
  6. <script type="text/javascript">
  7. function requerir(){
  8.     try{
  9.     req=new XMLHttpRequest();
  10.     }catch(err1){
  11.         try{
  12.         req=new ActiveXObject("Microsoft.XMLHTTP");
  13.         }catch(err2){
  14.             try{
  15.             req=new ActiveXObject("Msxml2.XMLHTTP");
  16.             }catch(err3){
  17.             req= false;
  18.             }
  19.         }
  20.     }
  21. return req;
  22. }
  23. var peticion=requerir();
  24.  
  25. function llamarAjax(){
  26. var aleatorio=parseInt(Math.random()*999999999);
  27. var campo=document.form1.apellido.value;
  28. var campo2=document.form1.nombre.value;
  29. var url="codigo.php?var="+campo+"&r="+aleatorio+"&c2="+campo2;
  30. peticion.open("GET",url,true);
  31. peticion.onreadystatechange =respuestaAjax;
  32. peticion.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
  33. peticion.send(null);
  34. }
  35.  
  36. function respuestaAjax(){
  37.     if(peticion.readyState==4)
  38.     {
  39.         if(peticion.status==200)
  40.         {
  41.         //alert(peticion.responseText);
  42.        
  43.      
  44.         var dina =  peticion.responseXML.getElementsByTagName("var")[0];
  45.         var apellidoDiv=  peticion.responseXML.getElementsByTagName("c2")[0];
  46.  
  47.            document.getElementById("dina").innerHTML=dina.childNodes[0].nodeValue;
  48.            document.getElementById("apellidoDiv").innerHTML=apellidoDiv.childNodes[0].nodeValue;
  49.         }
  50.         else
  51.         {
  52.         alert("ha ocurrido un error"+peticion.statusText);
  53.         }
  54.     }
  55.     else
  56.     {
  57.     document.getElementById("dina").innerHTML='<img src="carga.gif" />';
  58.     }
  59. }
  60.  
  61.  
  62. </script>
  63. </head>
  64. <body>
  65. <div id="dina"></div>
  66. <form action="" method="post" enctype="application/x-www-form-urlencoded" name="form1" id="form1">
  67. <label>
  68. <input type="text" name="nombre" id="nombre" />
  69. <div id="dina"></div><!--aqui quiero traer de la pagina solo la variable  $_GET['var']; -->
  70. </label>
  71. <label>
  72. <input type="text" name="apellido" id="apellido" />
  73. <div id="apellidoDiv"></div> <!--aqui quiero traer de la pagina solo la variable  $_GET['c2'];-->
  74. </label>
  75.  
  76. <input name="" type="button" onclick="llamarAjax()" />
  77. </form>
  78. </body>
  79.  
  80. </html>

Código PHP:
Ver original
  1. <?php
  2.  
  3. header('Content-Type: text/xml');
  4. echo "<?xml version=\"1.0\"?><respuesta><var>".$_GET['var']."</var><c2>".$_GET['c2']."</c2></respuesta>";
  5.  
  6. ?>


FUNCIONA DE MARAVILLA GENIOS POR FIN ESTO ES MUY PADRE
  #19 (permalink)  
Antiguo 29/06/2013, 02:20
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: tengo un problema muy interesante sobre ajax

Hola:

Personalmente prefiero las respuestas Ajax con formato XML, sobre todo porque consigo un sistema accesible.

Pero aunque es notoriamente fácil de generar, hay veces que necesitas obtener datos con contenido que malforma la salida, así que debes pensar en un sistema de blindaje, para contenido dentro de etiquetas se usan los "CDATA"s, y en los atributos (pueden simplificar bastante el código) considerar las entidades (entities), o sea puedes usar htmlentities o según el caso urlencode en php...

Un ejemplo de blindaje lo muestro en este artículo/ejemplo: Destripando objetos Ajax, en la pestaña: "Errores XML".

Saludos
__________________
Por favor:
No hagan preguntas de temas de foros en mensajes privados... no las respondo
  #20 (permalink)  
Antiguo 02/07/2013, 11:28
Avatar de Reedyseth  
Fecha de Ingreso: enero-2009
Ubicación: Chihuahua, México
Mensajes: 419
Antigüedad: 15 años, 3 meses
Puntos: 36
Respuesta: tengo un problema muy interesante sobre ajax

Felicidades Caricatos con tu página de Ajax, parece que le invertiste bastante tiempo,

Saludos
__________________
Reedyseth
Te ayudo? No olvides dar un +
blog:http://behstant.com/blog
En el blog:Tutoriales de Desarrollo Web PHP, Javascript, BD y más.

Etiquetas: ajax
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 05:32.