Foros del Web » Programando para Internet » Javascript »

new net.CargadorContenidos

Estas en el tema de new net.CargadorContenidos en el foro de Javascript en Foros del Web. Hola, Estoy empezando con ajax y php y me da el siguiente error Se ha producido un error al obtener los datos readyState:1 status: 0 ...
  #1 (permalink)  
Antiguo 10/10/2011, 11:32
 
Fecha de Ingreso: septiembre-2006
Mensajes: 114
Antigüedad: 17 años, 7 meses
Puntos: 1
new net.CargadorContenidos

Hola,

Estoy empezando con ajax y php y me da el siguiente error

Se ha producido un error al obtener los datos

readyState:1
status: 0
headers: null

Os adjunto mi codigo:

El fichero ejercicio1a.php

Código HTML:
<html>
<head>
<script language="javascript" src="./ajax.js" type=text/javascript></script>
<script type="text/javascript">
    //Validacion de numero
    function isNumero(num){
        if(/^([0-9])*$/.test(num)) {
            return true;
        }else{
            return false;
        }
    }

    function cargarTablaMultiplicar() {
        var form = document.getElementById('formularioMultiplicar');
        var numero =  form.numero.value;
        if(numero!='' && isNumero(numero)){
            var url ='<?php echo $HTTP_REFERER ?>' +'tablaMultiplicarAjax.php?numero='+numero;
            //"application/x-www-form-urlencoded"
            new net.CargadorContenidos (url, muestraContenido, null, 'GET', null,null);
        }else{
            alert('Debe introducir un numero');
        }
        
    }

    function muestraContenido   () {
        var divTabla = document.getElementById('tablaMultiplicar');
        divTabla.innerHTML = this.req.responseText;
    }
    
    function limpiarFormulario() {
        var form = document.getElementById('formularioMultiplicar');
        form.reset();
        var divTabla = document.getElementById('tablaMultiplicar');
        divTabla.innerHTML = "";
    }

</script>

<title>Ejercicio 1</title>
</head>
<body>
<center>
<form method="get" id="formularioMultiplicar">
Numero: <input type="text" name="numero" ></br></br>
<a href="javascript:cargarTablaMultiplicar();">Obtener tabla de multiplicar</a>
<a href="javascript:limpiarFormulario();">Limpiar</a>
</form>
<div id="tablaMultiplicar"></div>
</center>
</body>
</html> 
El fichero ajax.js

Código HTML:
var net = new Object();

net.READY_STATE_UNINITIALIZED=0;
net.READY_STATE_LOADING=1;
net.READY_STATE_LOADED=2;
net.READY_STATE_INTERACTIVE=3;
net.READY_STATE_COMPLETE=4;

// Constructor
net.CargadorContenidos = function(url, funcion, funcionError, metodo, parametros,contentType) {
    this.url = url;
    this.req = null;
    this.onload = funcion;
    this.onerror = (funcionError) ? funcionError : this.defaultError;
    this.cargaContenidoXML(url, metodo, parametros, contentType);
};

net.CargadorContenidos.prototype = {
    cargaContenidoXML: function(url, metodo, parametros, contentType) {
        if(window.XMLHttpRequest) {
            this.req = new XMLHttpRequest();
        }else if(window.ActiveXObject) {
            this.req = new ActiveXObject("Microsoft.XMLHTTP");
        }
        if(this.req) {
            try {
                var loader = this;
                this.req.onreadystatechange = function() {
                    loader.onReadyState.call(loader);
                };
                this.req.open(metodo, url, true);
                if(contentType) {
                    this.req.setRequestHeader("Content-Type", contentType);
                }
                this.req.send(parametros);
            } catch(err) {
                this.onerror.call(this);
            }
        }
    },
    onReadyState: function() {
        var req = this.req;
        var ready = req.readyState;
        if(ready == net.READY_STATE_COMPLETE) {
            var httpStatus = req.status;
            if(httpStatus == 200 || httpStatus == 0) {
                this.onload.call(this);
            }
            else {
                this.onerror.call(this);
            }
        }
    },
    defaultError: function() {
        alert("Se ha producido un error al obtener los datos"
                + "\n\nreadyState:" + this.req.readyState
                + "\nstatus: " + this.req.status
                + "\nheaders: " + this.req.getAllResponseHeaders());
    }
};
Y el fichero de recarga tablaMultiplicarAjax.php

Código HTML:
<center>
<?php
$metodo=$_SERVER['REQUEST_METHOD'];
echo "<h2>Tabla del $_GET[numero] </h2>";
for($i=1;$i<=10;$i++){
$resultado=$i*$_GET[numero];
echo "<b>$_GET[numero] x $i = $resultado</b></br>";
}
?>
</center> 
Gracias
  #2 (permalink)  
Antiguo 10/10/2011, 11:38
Avatar de pateketrueke
Modernizr
 
Fecha de Ingreso: abril-2008
Ubicación: Mexihco-Tenochtitlan
Mensajes: 26.399
Antigüedad: 16 años
Puntos: 2534
Respuesta: new net.CargadorContenidos

Pues el error es de Javascript y no de PHP, deberías aprender también las diferencias entre ambos.

Te sugiero uses Firefox+Firebug para depurar tu Javascript, y cuando tengas un error que si sea de PHP no dudes en preguntar.
__________________
Y U NO RTFM? щ(ºдºщ)

No atiendo por MP nada que no sea personal.
  #3 (permalink)  
Antiguo 10/10/2011, 11:43
 
Fecha de Ingreso: septiembre-2006
Mensajes: 114
Antigüedad: 17 años, 7 meses
Puntos: 1
Respuesta: new net.CargadorContenidos

Perdon, como puedo moverlo para el foro de javascript, estoy debugeando y he visto en la linea que es pero no llego a entender lo que sucede

this.req.send(parametros);

ya que no tengo parámetros, estos estan en la url.

Gracias

Etiquetas: html, tabla
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 10:55.