Loading ...
Sorry, an error occurred while loading the content.

YUI: Connection manager problem, variable scoping?

Expand Messages
  • dcinteractive123
    I am trying to get a simple HTTP get to work. I took the GET example and made slight modifications. No matter what I try, I cannot access the o.responseText
    Message 1 of 4 , Aug 30, 2007
    • 0 Attachment
      I am trying to get a simple HTTP get to work. I took the GET example and made slight modifications. 
      No matter what I try, I cannot access the o.responseText data outside of the handleSuccess function.
      What am I doing wrong? I have tried to set the data to a global value, used return values, etc.
      I hope that I am missing some simple scoping rule.


      var response = "test";

      var handleSuccess = function(o){
      if(o.responseText !== undefined){
      response = o.responseText;
      //will return the correct length
      alert("response text length " + response.length);
      }
      }

      var handleFailure = function(o){
      if(o.responseText !== undefined){
      }
      }

      var callback =
      {
      success:handleSuccess,
      failure: handleFailure
      argument: { foo:"foo", bar:"bar" }
      };


      var request = YAHOO.util.Connect.asyncRequest('GET', sUrl, callback);
      //will 4 - the length of "Test"
      alert("response text length " + response.length);


    • Satyam
      The execution of the callback after calling asyncRequest is, as the name implies, asynchronous so immediately after calling it, the response has not arrived
      Message 2 of 4 , Aug 30, 2007
      • 0 Attachment
        The execution of the callback after calling asyncRequest is, as the name implies, asynchronous so immediately after calling it, the response has not arrived yet and the last alert will show 4.  handleSuccess will, in time, asign the responseText to response, but your alert would have been executed by then.
         
        Satyam
         
        ----- Original Message -----
        Sent: Thursday, August 30, 2007 10:17 PM
        Subject: [ydn-javascript] YUI: Connection manager problem, variable scoping?

        I am trying to get a simple HTTP get to work. I took the GET example and made slight modifications. 
        No matter what I try, I cannot access the o.responseText data outside of the handleSuccess function.
        What am I doing wrong? I have tried to set the data to a global value, used return values, etc.
        I hope that I am missing some simple scoping rule.


        var response = "test";

        var handleSuccess = function(o){
        if(o.responseText !== undefined){
        response = o.responseText;
        //will return the correct length
        alert("response text length " + response.length);
        }
        }

        var handleFailure = function(o){
        if(o.responseText !== undefined){
        }
        }

        var callback =
        {
        success:handleSuccess,
        failure: handleFailure
        argument: { foo:"foo", bar:"bar" }
        };


        var request = YAHOO.util.Connect.asyncRequest('GET', sUrl, calback);
        //will 4 - the length of "Test"
        alert("response text length " + response.length);



        No virus found in this incoming message.
        Checked by AVG Free Edition.
        Version: 7.5.484 / Virus Database: 269.12.12/979 - Release Date: 29/08/2007 20:21
      • dcinteractive123
        Hi Satyam, Thank you for reply. Is there anyway to save the response from responseText and process it later? As far as I can tell, I must process the data in
        Message 3 of 4 , Aug 30, 2007
        • 0 Attachment
          Hi Satyam,

          Thank you for reply. Is there anyway to save the response from
          responseText and process it later? As far as I can tell, I must process
          the data in the handleSuccess function.

          --- In ydn-javascript@yahoogroups.com, "Satyam" <satyam@...> wrote:
          >
          > The execution of the callback after calling asyncRequest is, as the
          name implies, asynchronous so immediately after calling it, the response
          has not arrived yet and the last alert will show 4. handleSuccess will,
          in time, asign the responseText to response, but your alert would have
          been executed by then.
          >
          > Satyam
          >
          > ----- Original Message -----
          > From: dcinteractive123
          > To: ydn-javascript@yahoogroups.com
          > Sent: Thursday, August 30, 2007 10:17 PM
          > Subject: [ydn-javascript] YUI: Connection manager problem, variable
          scoping?
          >
          >
          > I am trying to get a simple HTTP get to work. I took the GET example
          and made slight modifications. No matter what I try, I cannot access the
          o.responseText data outside of the handleSuccess function.What am I
          doing wrong? I have tried to set the data to a global value, used return
          values, etc. I hope that I am missing some simple scoping rule.var
          response = "test";var handleSuccess = function(o){ if(o.responseText !==
          undefined){ response = o.responseText; //will return the correct length
          alert("response text length " + response.length); }}var handleFailure =
          function(o){ if(o.responseText !== undefined){ }}var callback ={
          success:handleSuccess, failure: handleFailure argument: { foo:"foo",
          bar:"bar" }};var request = YAHOO.util.Connect.asyncRequest('GET', sUrl,
          calback); //will 4 - the length of "Test" alert("response text length "
          + response.length);
          >
          >
          >
          ------------------------------------------------------------------------\
          ------
          >
          >
          > No virus found in this incoming message.
          > Checked by AVG Free Edition.
          > Version: 7.5.484 / Virus Database: 269.12.12/979 - Release Date:
          29/08/2007 20:21
          >
        • Satyam
          The data IS saved and you can process it later, but not before it has arrived. The simplest solution for you would be to process it in handleSuccess, since
          Message 4 of 4 , Aug 30, 2007
          • 0 Attachment
            The data IS saved and you can process it later, but not before it has
            arrived. The simplest solution for you would be to process it in
            handleSuccess, since that is the callback that signals the data has arrived.
            Of course, this will probably make things complicated elsewhere in your
            program.

            Concurrency problems have never been trivial, even though JavaScript makes
            it quite simple, being single-threaded something you can be sure is that
            handleSuccess will never be executed until AFTER the alert at the end is
            processed and finished. In other multithreaded environments, the main
            thread might be delayed by some circumstance and give handleSuccess a chance
            to be executed on a separate thread and so the data, though unlikely, might
            be ready. This is not the case with JavaScript. The bottom line of this
            is, things could be even worst!

            Programing with asynchronous events is hard to get used to but at some point
            you simply get it and make it work.

            Satyam

            ----- Original Message -----
            From: "dcinteractive123" <dcinteractive123@...>
            To: <ydn-javascript@yahoogroups.com>
            Sent: Thursday, August 30, 2007 10:46 PM
            Subject: [ydn-javascript] Re: YUI: Connection manager problem, variable
            scoping?


            > Hi Satyam,
            >
            > Thank you for reply. Is there anyway to save the response from
            > responseText and process it later? As far as I can tell, I must process
            > the data in the handleSuccess function.
            >
            > --- In ydn-javascript@yahoogroups.com, "Satyam" <satyam@...> wrote:
            >>
            >> The execution of the callback after calling asyncRequest is, as the
            > name implies, asynchronous so immediately after calling it, the response
            > has not arrived yet and the last alert will show 4. handleSuccess will,
            > in time, asign the responseText to response, but your alert would have
            > been executed by then.
            >>
            >> Satyam
            >>
            >> ----- Original Message -----
            >> From: dcinteractive123
            >> To: ydn-javascript@yahoogroups.com
            >> Sent: Thursday, August 30, 2007 10:17 PM
            >> Subject: [ydn-javascript] YUI: Connection manager problem, variable
            > scoping?
            >>
            >>
            >> I am trying to get a simple HTTP get to work. I took the GET example
            > and made slight modifications. No matter what I try, I cannot access the
            > o.responseText data outside of the handleSuccess function.What am I
            > doing wrong? I have tried to set the data to a global value, used return
            > values, etc. I hope that I am missing some simple scoping rule.var
            > response = "test";var handleSuccess = function(o){ if(o.responseText !==
            > undefined){ response = o.responseText; //will return the correct length
            > alert("response text length " + response.length); }}var handleFailure =
            > function(o){ if(o.responseText !== undefined){ }}var callback ={
            > success:handleSuccess, failure: handleFailure argument: { foo:"foo",
            > bar:"bar" }};var request = YAHOO.util.Connect.asyncRequest('GET', sUrl,
            > calback); //will 4 - the length of "Test" alert("response text length "
            > + response.length);
            >>
            >>
            >>
            > ------------------------------------------------------------------------\
            > ------
            >>
            >>
            >> No virus found in this incoming message.
            >> Checked by AVG Free Edition.
            >> Version: 7.5.484 / Virus Database: 269.12.12/979 - Release Date:
            > 29/08/2007 20:21
            >>
            >
            >
            >
            >
            >
            > Yahoo! Groups Links
            >
            >
            >
            >
            >
            > --
            > No virus found in this incoming message.
            > Checked by AVG Free Edition.
            > Version: 7.5.484 / Virus Database: 269.12.12/979 - Release Date:
            > 29/08/2007 20:21
            >
            >
          Your message has been successfully submitted and would be delivered to recipients shortly.