Foros del Web » Programando para Internet » Javascript »

problema ajax

Estas en el tema de problema ajax en el foro de Javascript en Foros del Web. estimados tengo un código en php el cual genera una coordenada y la guarda en un archivo de texto, luego con ajax lo obtengo y ...
  #1 (permalink)  
Antiguo 20/06/2016, 20:27
alvaro_trewhela
Invitado
 
Mensajes: n/a
Puntos:
problema ajax

estimados tengo un código en php el cual genera una coordenada y la guarda en un archivo de texto, luego con ajax lo obtengo y vuelvo a guardar otro y se repite, les dejo el html y php

Código HTML:
Ver original
  1.     <head>
  2.         <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
  3.         <title>GAME</title>
  4.        
  5.         <script type="text/javascript">
  6.        
  7.        
  8.         function createREQ() {
  9.                 try {
  10.                 req = new XMLHttpRequest(); /* p.e. Firefox */
  11.                 }
  12.                 catch(err1){
  13.                     try {
  14.                     req = new ActiveXObject('Msxml2.XMLHTTP'); /* algunas versiones IE */
  15.                     }
  16.                     catch (err2) {
  17.                         try {
  18.                         req = new ActiveXObject("Microsoft.XMLHTTP"); /* algunas versiones IE */
  19.                         }
  20.                         catch (err3) {
  21.                         req = false;
  22.                         }
  23.                     }
  24.                 }
  25.         return req;
  26.         }
  27.        
  28.         http = new createREQ();
  29.    
  30.         function requestPOST(){
  31.         http.open("POST", "pos.php", true);
  32.         http.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
  33.         http.onreadystatechange = getpos;
  34.         http.send("");
  35.         }
  36.    
  37.         function getpos(){
  38.             if(http.readyState == 4){
  39.                 if(http.status == 200){
  40.                 xml = http.responseXML;
  41.                
  42.                 dato = xml.getElementsByTagName("pos")[0].childNodes[0].nodeValue;
  43.                
  44.                 document.getElementById("pos").innerHTML=dato;
  45.                 }
  46.             }
  47.         }
  48.         a = setInterval(requestPOST(), 1000);
  49.         </script>
  50.    
  51.     </head>
  52.     <body>
  53.         <div id="pos"></div>
  54.     </body>
  55. </html>

Código PHP:
Ver original
  1. <?php
  2.  
  3. $doc = "documento.txt";
  4.  
  5. $fp = fopen($doc, "w");
  6.  
  7. $r1 = rand(1,100);
  8. $r2 = rand(1,100);
  9. $rand = "$r1,$r2";
  10.  
  11. fwrite($fp, $rand, strlen($rand));
  12. fclose($fp);
  13.  
  14. $fp = fopen($doc, "r");
  15. $pos = fread($fp , filesize($doc));
  16. fclose($fp);
  17.  
  18. header("Content-type: text/xml");
  19.  
  20. echo '<?xml version="1.0" encoding="UTF-8" ?>
  21. <poss>
  22.     <pos>
  23.     '.$pos.'
  24.     </pos>
  25. </poss>';
  26. ?>

el problema es que el intervalo nunca sucede...
ayuda porfavor
  #2 (permalink)  
Antiguo 21/06/2016, 00: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: problema ajax

Hola:

Deberías quitar el juego de paréntesis en la llamada...

Saludos
__________________
Por favor:
No hagan preguntas de temas de foros en mensajes privados... no las respondo
  #3 (permalink)  
Antiguo 21/06/2016, 17:26
alvaro_trewhela
Invitado
 
Mensajes: n/a
Puntos:
Respuesta: problema ajax

Cita:
Iniciado por caricatos Ver Mensaje
Hola:

Deberías quitar el juego de paréntesis en la llamada...

Saludos
me funcionó

Etiquetas: ajax, html, php
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 19:27.