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 originalvar AJAX = new AJAX(datos,URL,charset);
var recibeDatos = AJAX.returnRequest();
Ahora sigue lo objeto.
Código Javascript
:
Ver originalfunction AJAX(dados, URL, codificacao){
this.xmlhttp = null;
this.returnRequest = null;
this.setRequest = null;
this.initRequest = null;
this.statusRequest = null;
this.url = null;
this.methodReq = null;
this.collectionsMethods = {_0:null,_1:null,_2:null,_3:null,_4:null};
this.objStatus = new Boolean(false);
var obj = this;
this.setRequest = function(){
this.defineMethod = null;
this.setRedHeader = null;
try{
this.xmlhttp = new XMLHttpRequest();
}catch(e){
this.xmlhttp = new ActiveXObject("MSXML2.XMLHTTP.3.0");
}
this.defineMethod = function(notAutoMethod){
if(notAutoMethod != null){
if(this.methodReq == "GET"){
dados == null ? this.url = URL : this.url = URL+"?"+dados;
}else{
this.url = URL;
this.methodReq = "POST"
}
}else{
if(dados.length >= 2048){
this.methodReq = new String("POST");
this.url = URL;
}else{
if(dados.length == 0){
this.methodReq = "GET";
this.url = URL;
dados = null;
}else{
this.methodReq = "GET";
this.url = URL + "?" + dados;
}
}
}
};
this.setReqHeader = function(header, value){
this.xmlhttp.setRequestHeader(header, value);
};
if(this.methodReq == null){
this.defineMethod(null);
}
};
this.initRequest = function(){
this.setRequest();
this.xmlhttp.onreadystatechange = function(){
statusRequest(obj);
};
this.xmlhttp.open(this.methodReq, this.url,true);
this.setReqHeader('Content-Type', 'text/xml');
this.setReqHeader('encoding', codificacao);
this.setReqHeader('Content-Type', 'application/x-www-form-urlencoded');
if(this.method == "POST"){
this.setReqHeader("Content-length", dados.length);
}
this.xmlhttp.send(dados);
};
this.statusRequest = function(obj){
switch(obj.xmlhttp.readyState){
case 0:
if(this.collectionsMethods._0){
this.collectionsMethods._0();
}
this.objStatus = false;
break;
case 1:
if(this.collectionsMethods._1){
this.collectionsMethods._1();
}
this.objStatus = false;
break;
case 2:
if(this.collectionsMethods._2){
this.collectionsMethods._2();
}
this.objStatus = false;
break;
case 3:
if(this.collectionsMethods._3){
this.collectionsMethods._3();
}
this.objStatus = false;
break;
case 4:
if(this.collectionsMethods._4){
this.collectionsMethods._4();
}
this.objStatus = true;
break;
default:
alert("Erro");
break;
}
};
this.returnRequest = function(){
try{
switch(this.objStatus){
case 404:
return new pack(404,"Not Found","Not Found",null);
break;
case 401:
return new pack(401,"Unauthorized","Unauthorized",null);
break;
case 500:
return new pack(500,"Internal Server Error","Internal Server Error",null);
break;
case 503:
return new pack(503,"Service Unavailable","Service Unavailable",null);
break;
case 200:
return new pack(200,"OK",this.xmlhttp.responseText,this.xmlhttp.responseXML);
break;
default:
return new pack(null,null,null,null);
break;
}
}catch(e){
alert(e);
return new pack(null,null,null,null);
}
function pack(value0,value1,value2,value3){
this.numberStatus = value0;
this.status = value1;
this.text = value2;
this.xml = value3;
}
}
this.initRequest();
}
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.