Foros del Web » Programando para Internet » Jquery »

No obtengo respuesta del objeto XMLhttpRequest

Estas en el tema de No obtengo respuesta del objeto XMLhttpRequest en el foro de Jquery en Foros del Web. Hola que tal amigos, miren estoy haciendo un plugin para adelantar trabajo basado en xhr level 2, en jquery, pero ocurre un inconveniente cuando llega ...
  #1 (permalink)  
Antiguo 27/06/2014, 21:29
 
Fecha de Ingreso: septiembre-2012
Ubicación: Buenos aires
Mensajes: 110
Antigüedad: 11 años, 7 meses
Puntos: 9
Pregunta No obtengo respuesta del objeto XMLhttpRequest

Hola que tal amigos, miren estoy haciendo un plugin para adelantar trabajo basado en xhr level 2, en jquery, pero ocurre un inconveniente cuando llega al punto del onload ajax no me retorna el valor de respuesta del archivo llamado pero si lo habre, busque por el codigo y no le veo el error por ningun lado ya hice alert con el objeto xhr strignificado y no se producen cambios desde el readystate(1,2,3)(procesando) hasta el 4(cargado)...

Código Javascript:
Ver original
  1. (function($){
  2.  
  3.     $.AjaxPrepare = function(options){
  4.    
  5.         var settings = jQuery.extend({
  6.            
  7.             // Propiedades
  8.             url         : '',
  9.             response    : 'text',
  10.             method      : 'POST',
  11.             crossDomain : false,
  12.             insert      : [],
  13.            
  14.             // Metodos
  15.             onProgress  : function(){},
  16.             onBefore    : function(){},
  17.             onLoaded    : function(){},
  18.             onError     : function(){},
  19.        
  20.         },options);
  21.  
  22.         try{
  23.            
  24.            
  25.             // Creamos nuestro xhr
  26.             var ajax  = createXHR(settings.crossDomain);
  27.            
  28.             // Preparamos objeto a enviar
  29.             var datos = prepareInsert(settings.method,settings.insert);
  30.            
  31.             // Antes de enviar
  32.             onBefore(settings.onBefore);
  33.            
  34.             // Open
  35.             open(ajax,settings.method,settings.url,datos);
  36.            
  37.             // Progress
  38.             onProgress(ajax,settings.onProgress);
  39.            
  40.             // Finalizado
  41.             onLoaded(ajax,settings.response,settings.onLoaded);
  42.            
  43.             // Envio
  44.             sendRequest(ajax,settings.method,datos);
  45.            
  46.         }catch(e){
  47.        
  48.             // Error
  49.             settings.onError(e);
  50.        
  51.         }
  52.        
  53.        
  54.     }
  55.  
  56.     // ----- Errores ------
  57.     error = {
  58.    
  59.         1: {
  60.                 name: '[ErrorCode: 1]',
  61.                 message: 'Surgio un error al obtener sus datos JSON.'
  62.             }
  63.    
  64.    
  65.    
  66.     }
  67.    
  68.     // ----- Metodos ------
  69.     onBefore        = function(functions){
  70.        
  71.         if(typeof functions == "function")
  72.             functions();
  73.        
  74.     }
  75.    
  76.     createXHR       = function(domain){
  77.    
  78.         if(domain == true && "withCredentials" in XMLHttpRequest)
  79.  
  80.             var xhr = new XMLHttpRequest();
  81.  
  82.         else if(domain==true && !"withCredentials" in XMLHttpRequest)
  83.  
  84.             var xhr = new XDomainRequest()
  85.  
  86.         else
  87.             var xhr = new XMLHttpRequest();
  88.  
  89.         return xhr;
  90.  
  91.     }
  92.  
  93.     prepareInsert   = function(method,datos){
  94.        
  95.         // POST
  96.         if(method.toLowerCase() =='post'){
  97.            
  98.             var data = new FormData;
  99.            
  100.             for(var i = 0; i < datos.length; i++){
  101.            
  102.                 data.append(datos[i][0], datos[i][1]);
  103.            
  104.             }
  105.            
  106.         }else{
  107.        
  108.             var data = new String('?');
  109.        
  110.             for(var i=0; i < datos.length; i++){
  111.            
  112.                 if(i  <datos.length)
  113.                     data += datos[i][0]+'='+datos[i][1]+'&';
  114.                 else
  115.                     data += datos[i][0]+'='+datos[i][1];
  116.            
  117.             }
  118.        
  119.         }
  120.        
  121.         return data;
  122.            
  123.     }
  124.  
  125.     open            = function(ajax,method,url,data){
  126.    
  127.         if(method.toLowerCase()=='post')
  128.             ajax.open(method,url,true);
  129.         else
  130.             ajax.open(method,url+data,true);
  131.    
  132.     }
  133.  
  134.     onProgress      = function(ajax,functions){
  135.        
  136.         if(typeof functions == "function")
  137.            
  138.             ajax.upload.onprogress = function(e){
  139.            
  140.             if(e.lengthComputable){
  141.                
  142.                 var porcentaje=Math.ceil((e.loaded/e.total)*100);
  143.                
  144.                 functions(porcentaje);
  145.            
  146.             }
  147.            
  148.         };
  149.        
  150.     }
  151.  
  152.     onLoaded        = function(ajax,response,functions){
  153.        
  154.         if(response.toLowerCase()=='json')
  155.            
  156.             if(typeof JSON.parse(ajax.response) =='object')
  157.                 ajax.onload = function(){
  158.                    
  159.                     functions(JSON.parse(ajax.response));
  160.                
  161.                 }
  162.                
  163.             else
  164.                 throw Error(error[1]);
  165.        
  166.         else
  167.            
  168.             ajax.onload = functions(ajax.response);
  169.                
  170.     }
  171.  
  172.     sendRequest     = function(ajax,method,datos){
  173.        
  174.         if(method.toLowerCase()=='post'){
  175.            
  176.             ajax.send(datos);
  177.            
  178.         }else{
  179.        
  180.             ajax.send(null);
  181.            
  182.         }
  183.        
  184.     }
  185.  
  186.  
  187.    
  188. })(jQuery);


Se llama asi
Código HTML:
Ver original
  1. <!doctype html>
  2.    
  3.     <script type="text/javascript" src="jquery.js"></script>
  4.     <script type="text/javascript" src="AjaxPrepare.js"></script>
  5.    
  6. </head>
  7.  
  8.     <div class="container">
  9.    
  10.         <output id="output"></output>
  11.        
  12.     </div>
  13.    
  14.     <script type="text/javascript">
  15.    
  16.            
  17.         function prueba(){
  18.  
  19.             $.AjaxPrepare({
  20.  
  21.                 url: 'prueba.php',
  22.                 response: 'text',
  23.                 method: 'post',
  24.  
  25.                 onBefore: function(){
  26.  
  27.                     // Procesando
  28.                     $('#output').html('Cargando...');
  29.  
  30.                 },
  31.  
  32.                 onLoaded: function(response){
  33.                    
  34.                     // Mostrando
  35.                     $('#output').html(response);
  36.  
  37.                 },
  38.  
  39.                 onError: function(e){
  40.  
  41.                     alert(e);
  42.  
  43.                 }
  44.  
  45.             });
  46.  
  47.  
  48.         }
  49.  
  50.         $(document).ready(function(){
  51.  
  52.             prueba();
  53.  
  54.         });
  55.  
  56.    
  57.     </script>
  58.    
  59. </body>

El archivo php
Código PHP:
Ver original
  1. <?php echo 'hola'; ?>

Etiquetas: javascript
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 02:57.