Object reference on the return from an AJAX call
0 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Jason_Cavaliere
Posted On:   Wednesday, May 17, 2006 11:24 AM

I'm having trouble with my object references. For instance, I call myobject.GetSomeAJAXData(); which inside says UtilClass.HTTP.SubmitXmlHttp( ) That part works ok. My problem is that when the data gets processed and returned from the server, it doesn't recognise what object it was soppoes to return to. say I have some object: function myObject(){   this.variable="hellow world!";   this.callbackfunction = function(response) {     alert(this.variable); } } Below is the Javascript function that I'm calling(It's in a library that I wrote, hence the prototyping) -   More>>

I'm having trouble with my object references. For instance, I call myobject.GetSomeAJAXData(); which inside says UtilClass.HTTP.SubmitXmlHttp( )



That part works ok. My problem is that when the data gets processed and returned from the server, it doesn't recognise what object it was soppoes to return to.



say I have some object:



function myObject(){

  this.variable="hellow world!";

  this.callbackfunction = function(response)

{

    alert(this.variable);

}



}





Below is the Javascript function that I'm calling(It's in a library that I wrote, hence the prototyping) - when I pass (into callBack) myObject.callbackfunction or "myObject.callbackfunction " it calls the function correctly, however when I reference "this", it refers to the window object, not myObject, thus throwing an error when trying to access this.variable Is there any way around this? Any help would be greatly appreciated!




UtilClass.prototype.HTTP.SubmitXmlHttp=function (method,url,data,callBack,callBackError,async)

{

  if (null == async) async = true;

  var xmlHttp = VW.HTTP.GetXmlHttp();//Gets Cross Browser COmpatable AJAX Object

  xmlHttp.onreadystatechange = function()

  {

    if (4 ==xmlHttp.readyState)

    {

    if (200 == xmlHttp.status)

      {

      if(null!=callBack)

      {

        var evaledCallback=eval(callBack);

        evaledCallbackxmlHttp.responseText);

      }

     }

     else if (callBackError != null)

     {

      callBackError(xmlHttp)

     }

     else

     {

      alert(xmlHttp.status + " : An error occurred. Please try again later.");

     }

    xmlHttp=null;

   }

   }

   xmlHttp.open(method, url, async);

   if (null != data)

   {

   data=encodeURI(data);

   xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");

   xmlHttp.setRequestHeader("Content-Length", data.length);

    }

  xmlHttp.send(data);

}

   <<Less
About | Sitemap | Contact