Hi, I am a newbie in the AJAX world. first of all, take a look at my script first:
assume this script is saved as "myAjax.js"
[CODE=javascript]//-----------------inside myAjax.js--------------------
function AJAX()
{
var isBusy = false;
var obj = null;
var url = "";
var Method = "";
var param = "";
var result = "";
var isReady;
if(window.XMLHt tpRequest)
this.obj = new XMLHttpRequest( );
else
{
try
{this.obj = new ActiveXObject(" MSXML2.XMLHTTP" );}
catch(exception )
{
try
{this.obj = new ActiveXObject(" Microsoft.XMLHT TP");}
catch(exception ){this.obj = null;}
}
}
this.isReady = false;
function setOpenProperti es(_method,_url ,_param)
{
this.url = _url;
this.Method = _method;
this.param = _param;
}
function getData(data)
{
if(this.obj)
{
if(isBusy)
{
window.status = "busy";
}
var tObj = this.obj;
if(this.Method. toUpperCase() == "POST")
{
this.obj.open(t his.Method,this .url);
this.obj.setReq uestHeader('Con tent-Type', 'application/x-www-form-urlencoded;');
}
else
{
if(this.param != "")
{this.obj.open( this.Method,thi s.url + "?" + this.param + data);}
else
{this.obj.open( this.Method,thi s.url + "?" + data);}
}
isBusy = true;
this.obj.onread ystatechange = function()
{
if(tObj.readySt ate == 4)
{
if(tObj.status == 200)
{
var temp = tObj.responseTe xt;
saveResult(temp );
isBusy = false;
}
}
}
if(this.Method. toUpperCase() == "POST")
{
data = this.param + encode(data);
this.obj.send(d ata);
}
else
{this.obj.send( null);}
}
}
function saveResult(_res ult)
{
this.obj.result = _result;
this.obj.isRead y = true;
}
function getResult()
{
alert(this.obj) ;
}
this.setOpenPro perties = setOpenProperti es;
this.getData = getData;
this.getResult = getResult;
}[/code]
//--------------------------End of script---------------------------
this AJAX function is called from another js file, assume myMain.js.
script in myMain.js is pretty simple(like below:
[code=javascript]//--------------Inside myMain.js------------------------
var count = 5;
var obj = new AJAX();
function display_thumbna il(htmlObj,appr oved_flag)
{
var str = "select p_thumImage from products where p_gallery = " + approved_flag;
obj.setOpenProp erties("POST"," callBackHandler .php","function =query&type=p_i mage&sql=")
obj.getData(str );
var isReady = false;
obj.getResult() ;
//alert(obj.getRe sult());
}[/CODE]
//------------------------End of script--------------------------
the function "display_thumbn ail" will be called as a part of mouseclick event in one of my html page.
As you can see in myMain.js, I am trying to get any data that is returned from the call by the XmlHttpRequest. responseText method which is called inside getData function declared in myAJAX.js, but I don't know how to do that "properly". the function getResult will return the data properly after the user clicks on the link which will call the "display_thumbn ail" function once again. In the other words, the first call to getResult always return nothing.
Can someone help me out with this???
assume this script is saved as "myAjax.js"
[CODE=javascript]//-----------------inside myAjax.js--------------------
function AJAX()
{
var isBusy = false;
var obj = null;
var url = "";
var Method = "";
var param = "";
var result = "";
var isReady;
if(window.XMLHt tpRequest)
this.obj = new XMLHttpRequest( );
else
{
try
{this.obj = new ActiveXObject(" MSXML2.XMLHTTP" );}
catch(exception )
{
try
{this.obj = new ActiveXObject(" Microsoft.XMLHT TP");}
catch(exception ){this.obj = null;}
}
}
this.isReady = false;
function setOpenProperti es(_method,_url ,_param)
{
this.url = _url;
this.Method = _method;
this.param = _param;
}
function getData(data)
{
if(this.obj)
{
if(isBusy)
{
window.status = "busy";
}
var tObj = this.obj;
if(this.Method. toUpperCase() == "POST")
{
this.obj.open(t his.Method,this .url);
this.obj.setReq uestHeader('Con tent-Type', 'application/x-www-form-urlencoded;');
}
else
{
if(this.param != "")
{this.obj.open( this.Method,thi s.url + "?" + this.param + data);}
else
{this.obj.open( this.Method,thi s.url + "?" + data);}
}
isBusy = true;
this.obj.onread ystatechange = function()
{
if(tObj.readySt ate == 4)
{
if(tObj.status == 200)
{
var temp = tObj.responseTe xt;
saveResult(temp );
isBusy = false;
}
}
}
if(this.Method. toUpperCase() == "POST")
{
data = this.param + encode(data);
this.obj.send(d ata);
}
else
{this.obj.send( null);}
}
}
function saveResult(_res ult)
{
this.obj.result = _result;
this.obj.isRead y = true;
}
function getResult()
{
alert(this.obj) ;
}
this.setOpenPro perties = setOpenProperti es;
this.getData = getData;
this.getResult = getResult;
}[/code]
//--------------------------End of script---------------------------
this AJAX function is called from another js file, assume myMain.js.
script in myMain.js is pretty simple(like below:
[code=javascript]//--------------Inside myMain.js------------------------
var count = 5;
var obj = new AJAX();
function display_thumbna il(htmlObj,appr oved_flag)
{
var str = "select p_thumImage from products where p_gallery = " + approved_flag;
obj.setOpenProp erties("POST"," callBackHandler .php","function =query&type=p_i mage&sql=")
obj.getData(str );
var isReady = false;
obj.getResult() ;
//alert(obj.getRe sult());
}[/CODE]
//------------------------End of script--------------------------
the function "display_thumbn ail" will be called as a part of mouseclick event in one of my html page.
As you can see in myMain.js, I am trying to get any data that is returned from the call by the XmlHttpRequest. responseText method which is called inside getData function declared in myAJAX.js, but I don't know how to do that "properly". the function getResult will return the data properly after the user clicks on the link which will call the "display_thumbn ail" function once again. In the other words, the first call to getResult always return nothing.
Can someone help me out with this???
Comment