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

Ayuda: Primer paso en AJAX

Estas en el tema de Ayuda: Primer paso en AJAX en el foro de Frameworks JS en Foros del Web. Gente, no logro dar mi primer paso en AJAX. Tengo tres páginas. 1- Index.htm Código: <html> <head> <script language="JavaScript" type="text/javascript" src="ajax.js"></script> </head> <body> <div align="center"> ...
  #1 (permalink)  
Antiguo 29/12/2009, 13:31
 
Fecha de Ingreso: mayo-2003
Mensajes: 312
Antigüedad: 21 años
Puntos: 2
Ayuda: Primer paso en AJAX

Gente, no logro dar mi primer paso en AJAX.

Tengo tres páginas.

1- Index.htm
Código:
<html>

<head>
 <script language="JavaScript" type="text/javascript" src="ajax.js"></script>
</head>

<body>
<div align="center">
<table border="0" width="558" cellspacing="0" cellpadding="0" id="table1">
	<tr>
		<td>
		<div id="resultado" name="resultado">Aca resultado</div>
		</td>
		<td>&nbsp;</td>
	</tr>
	<tr>
		<td>&nbsp;</td>
		<td>&nbsp;</td>
	</tr>
	<tr>
		<td><a href="#" onClick="pasonumero(1);">1</a></td>
		<td>&nbsp;</td>
	</tr>
</table>
</div>

</body>

</html>
2- Página: ajax.js
Código:
<script>
function objetoAjax()
{  
var xmlhttp=false;  
try 
{  
xmlhttp = new ActiveXObject("Msxml2.XMLHTTP"); 
 } 
 catch (e) 
 {  
 try {  
 xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");  
 } 
 catch (E) {  
 xmlhttp = false;  
 }  
 }  
 if (!xmlhttp && typeof XMLHttpRequest!='undefined') 
 {  
 xmlhttp = new XMLHttpRequest(); 
 }  
 return xmlhttp;
 }
///////////////////////////////////////////////////////////////

function pasonumero(id)
{

divResultado = document.getElementById('resultado');

ajax=objetoAjax();
ajax.open("POST","numero.php",true);

ajax.onreadystatechange=function() 
{  
if (ajax.readyState==4) {
divResultado.innerHTML = ajax.responseText
}
}

ajax.send("id="+id)
}


</script>
3- Página: numero.php
Código:
$id = $_POST["id"];
echo $id;
Al hacer click sobre el 1 del INDEX.HTM, me sale error. Alguno me podria dar una mano.
Gracias.
  #2 (permalink)  
Antiguo 29/12/2009, 21:08
Avatar de jackson666  
Fecha de Ingreso: noviembre-2009
Ubicación: Buenos Aires, Argentina
Mensajes: 1.971
Antigüedad: 14 años, 6 meses
Puntos: 65
Respuesta: Ayuda: Primer paso en AJAX

Hola que tal Dago
Te corrijo algunas cosas:

Código Javascript:
Ver original
  1. <script type="text/javascript">
  2. var xmlhttp=false;
  3. function objetoAjax(){  
  4.  
  5.     try{  
  6.         xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
  7.     }catch (e){  
  8.          try{  
  9.              xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");  
  10.        
  11.         // No hacia falta que en el bloque de captura
  12.         // le reasignes nuevamente el valor a xmlhttp.
  13.         // Pues si hasta aca no tiene un objeto dentro,
  14.         // seguira siendo false. Se entiende?
  15.         }catch (e) {
  16.      
  17.             //podrias hacer esto
  18.             alert('Tu navegador no soporta AJAX');
  19.             return false;
  20.         }  
  21.  }  
  22.  
  23.  if (!xmlhttp && typeof XMLHttpRequest!='undefined'){  
  24.  
  25.  xmlhttp = new XMLHttpRequest();
  26.  
  27. }  
  28.  
  29.  return xmlhttp;
  30.  
  31.  }
  32.  
  33. function pasonumero(id){
  34.  
  35. // supongo que el parametro 'id'
  36. // es un numero que le queres pasar
  37. // a tu script php
  38.  
  39. var divResultado = document.getElementById('resultado');
  40.  
  41. ajax=objetoAjax();
  42.  
  43. //no se que tipo y cantidad de informacion
  44. // le vas a pasar a tu php, pero te conviene
  45. // usar el metodo GET en este caso
  46.  
  47.  
  48. //fijate la forma de pasar la variable
  49. ajax.open("GET","numero.php?num="+id);
  50.  
  51. ajax.onreadystatechange=function(){  
  52.  
  53. if (ajax.readyState==4) {
  54.     if(ajax.status == 200){
  55.  
  56.         divResultado.innerHTML = ajax.responseText;
  57.  
  58.     }
  59. }
  60. }
  61.  
  62. //a las cabeceras no se deberia mandar nada
  63. ajax.send(null);
  64. }
  65. </script>

Luego deberias llamar a la funcion pasonumero() desde un link, o mediante algun evento. Para levantar el dato en tu php, solo deberias usar algo asi como $_GET['num'];

Espero que sirva
__________________
HV Studio
Diseño y desarrollo web
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 16:44.