Ver Mensaje Individual
  #3 (permalink)  
Antiguo 17/09/2010, 07:25
flpms
 
Fecha de Ingreso: marzo-2009
Ubicación: Santo André - São Paulo - Brasil
Mensajes: 91
Antigüedad: 16 años
Puntos: 1
Respuesta: Ejecucion en paralelo de functions

No pues lo problema no esta en la requisicion y si en la chamada de funcion externa.

Vou poner todo lo codigo para que puedas compreender mejor.

Pero antes voy detallar mi idea.

Yo quiero hacer uno objeto AJAX que con dos lineas de codigo haga toda la peticion y retorne los datos.

entonces es algo así llamo lo objeto con los parametros

Código Javascript:
Ver original
  1. var AJAX = new AJAX(datos,URL,charset);
  2. var recibeDatos = AJAX.returnRequest();


Ahora sigue lo objeto.

Código Javascript:
Ver original
  1. function AJAX(dados, URL, codificacao){
  2.     this.xmlhttp = null;
  3.     this.returnRequest = null;
  4.     this.setRequest = null;
  5.     this.initRequest = null;
  6.     this.statusRequest = null;
  7.     this.url = null;
  8.     this.methodReq = null;
  9.     this.collectionsMethods = {_0:null,_1:null,_2:null,_3:null,_4:null};
  10.     this.objStatus = new Boolean(false);
  11.     var obj = this;
  12.  
  13.     this.setRequest = function(){
  14.         this.defineMethod = null;
  15.         this.setRedHeader = null;
  16.         try{
  17.             this.xmlhttp = new XMLHttpRequest();
  18.         }catch(e){
  19.             this.xmlhttp = new ActiveXObject("MSXML2.XMLHTTP.3.0");
  20.         }
  21.  
  22.         this.defineMethod = function(notAutoMethod){
  23.             if(notAutoMethod != null){
  24.                 if(this.methodReq == "GET"){
  25.                     dados == null ? this.url = URL : this.url = URL+"?"+dados;
  26.                 }else{
  27.                     this.url = URL;
  28.                     this.methodReq = "POST"
  29.                 }
  30.             }else{
  31.                 if(dados.length >= 2048){
  32.                     this.methodReq = new String("POST");
  33.                     this.url = URL;
  34.                 }else{
  35.                     if(dados.length == 0){
  36.                         this.methodReq = "GET";
  37.                         this.url = URL;
  38.                         dados = null;
  39.                     }else{
  40.                         this.methodReq = "GET";
  41.                         this.url = URL + "?" + dados;
  42.                     }
  43.                 }
  44.             }
  45.         };
  46.         this.setReqHeader = function(header, value){
  47.             this.xmlhttp.setRequestHeader(header, value);
  48.         };
  49.  
  50.         if(this.methodReq == null){
  51.             this.defineMethod(null);
  52.         }
  53.  
  54.        
  55.     };
  56.  
  57.     this.initRequest = function(){
  58.         this.setRequest();
  59.         this.xmlhttp.onreadystatechange = function(){
  60.             statusRequest(obj);
  61.         };
  62.         this.xmlhttp.open(this.methodReq, this.url,true);
  63.         this.setReqHeader('Content-Type', 'text/xml');
  64.         this.setReqHeader('encoding', codificacao);
  65.         this.setReqHeader('Content-Type', 'application/x-www-form-urlencoded');
  66.         if(this.method == "POST"){
  67.             this.setReqHeader("Content-length", dados.length);
  68.         }
  69.         this.xmlhttp.send(dados);
  70.     };
  71.    
  72.     this.statusRequest = function(obj){
  73.         switch(obj.xmlhttp.readyState){
  74.             case 0:
  75.                 if(this.collectionsMethods._0){
  76.                     this.collectionsMethods._0();
  77.                 }
  78.                 this.objStatus = false;
  79.                 break;
  80.             case 1:
  81.                 if(this.collectionsMethods._1){
  82.                     this.collectionsMethods._1();
  83.                 }
  84.                 this.objStatus = false;
  85.                 break;
  86.             case 2:
  87.                 if(this.collectionsMethods._2){
  88.                     this.collectionsMethods._2();
  89.                 }
  90.                 this.objStatus = false;
  91.                 break;
  92.             case 3:
  93.                 if(this.collectionsMethods._3){
  94.                     this.collectionsMethods._3();
  95.                 }
  96.                 this.objStatus = false;
  97.                 break;
  98.             case 4:
  99.                 if(this.collectionsMethods._4){
  100.                     this.collectionsMethods._4();
  101.                 }
  102.                 this.objStatus = true;
  103.                 break;
  104.             default:
  105.                 alert("Erro");
  106.                 break;
  107.         }
  108.     };
  109.  
  110.     this.returnRequest = function(){
  111.         try{
  112.             switch(this.objStatus){
  113.                 case 404:
  114.                     return new pack(404,"Not Found","Not Found",null);
  115.                     break;
  116.                 case 401:
  117.                     return new pack(401,"Unauthorized","Unauthorized",null);
  118.                     break;
  119.                 case 500:
  120.                     return new pack(500,"Internal Server Error","Internal Server Error",null);
  121.                     break;
  122.                 case 503:
  123.                     return new pack(503,"Service Unavailable","Service Unavailable",null);
  124.                     break;
  125.                 case 200:
  126.                     return new pack(200,"OK",this.xmlhttp.responseText,this.xmlhttp.responseXML);
  127.                     break;
  128.                 default:
  129.                     return new pack(null,null,null,null);
  130.                     break;
  131.             }
  132.         }catch(e){
  133.             alert(e);
  134.             return new pack(null,null,null,null);
  135.         }
  136.  
  137.         function pack(value0,value1,value2,value3){
  138.         this.numberStatus = value0;
  139.         this.status = value1;
  140.         this.text = value2;
  141.         this.xml = value3;
  142.         }
  143.     }
  144.  
  145.     this.initRequest();
  146. }

Durante la llamada de objeto este hace todo lo que necessta para una peticion. Durante la ejecucion de peticion no queda en objeto para lo retorno de los datos. e logo sigue para la function AJAX.returnRequest(); solo que sin datos niguno.
__________________
Exitem 11 tipos de personas. Las personas que comprendem binario. Y las personas que no comprende.

Tudo en la vida és sencillo o muy sencillo.

Última edición por flpms; 17/09/2010 a las 07:36 Razón: poner los codigo