Ver Mensaje Individual
  #4 (permalink)  
Antiguo 18/11/2013, 18:37
Avatar de utan
utan
 
Fecha de Ingreso: agosto-2012
Mensajes: 126
Antigüedad: 11 años, 9 meses
Puntos: 17
Respuesta: Descarga de archivo con AJAX

Hola,

A mi me trabaja tu ejemplo bien, que browser estas usando..

solo definí la variable que no definiste para que solo exista en el scope de tu función principal..
Y hice el path abstracto, eso por si esque el browser este asiendo el request como a otro dominio el cual no es permitido a menos que hagas Jsonp.
Código Javascript:
Ver original
  1. <script>
  2. function descargaArchivo() {
  3.     var peticion_http = null;
  4.     // Obtener la instancia del objeto XMLHttpRequest
  5.     if(window.XMLHttpRequest) {
  6.         peticion_http = new XMLHttpRequest();
  7.     }
  8.     else if(window.ActiveXObject) {
  9.         peticion_http = new ActiveXObject("Microsoft.XMLHTTP");
  10.     }
  11.     // Preparar la funcion de respuesta
  12.     peticion_http.onreadystatechange = muestraContenido;
  13.     // Realizar peticion HTTP
  14.     peticion_http.open('GET', 'MiAplicacionWeb/TextFiles/HolaMundo.txt', true);
  15.     peticion_http.send(null);
  16.    
  17.     function muestraContenido() {
  18.        
  19.         if(peticion_http.readyState == 4) {
  20.             if(peticion_http.status == 200) {
  21.                
  22.                 alert(peticion_http.responseText);
  23.             }
  24.         }
  25.     }
  26. }
  27. window.onload = descargaArchivo();
  28. </script>

Para no tener la variable en el scope de tu función principal and no queres crear otra función podes usarla así también..

Código Javascript:
Ver original
  1. <script>
  2. function descargaArchivo() {
  3.     var peticion_http = null;
  4.     // Obtener la instancia del objeto XMLHttpRequest
  5.     if(window.XMLHttpRequest) {
  6.         peticion_http = new XMLHttpRequest();
  7.     }
  8.     else if(window.ActiveXObject) {
  9.         peticion_http = new ActiveXObject("Microsoft.XMLHTTP");
  10.     }
  11.     // Preparar la funcion de respuesta
  12.     peticion_http.onreadystatechange = function () {
  13.        
  14.         if(this.readyState == 4) {
  15.             if(this.status == 200) {
  16.                
  17.                 alert(this.responseText);
  18.             }
  19.         }
  20.     };
  21.     // Realizar peticion HTTP
  22.     peticion_http.open('GET', 'MiAplicacionWeb/TextFiles/HolaMundo.txt', true);
  23.     peticion_http.send(null);
  24.    
  25. }
  26. window.onload = descargaArchivo();
  27. </script>

Asi no clogeas el scope de tu función principal y mas legible me parece..
__________________
Mis conocimientos son limitado, pero si te puedo ayudar lo are gustoso mi chat particular, visitalo gracias http://rendezvouschat.com

Última edición por utan; 18/11/2013 a las 18:45