Javascript variable scope issue, prototype.js

Hi all,

I have one function

function GetData()
{
      var resp;
      new Ajax.Request("value.php",
             {
                              method:'get',
                              onSuccess: function(transport){
                              response = transport.responseText || "";      
                  
                              alert(response); // 1            
                                       
                                        resp  = response;
                },
                        
                onFailure: function(){  }
              });
alert resp;
}

/////////////

the problem

alert (response) works fine
but
alert(resp) doesnt work fine. I want the value of response in resp.

Hope its clear. Thanks in advance.
kiranvj
LVL 16
Kiran Paul VJComputer EngineerAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

LakioCommented:
Im guessing you just forgot () "alert resp" -- > alert(resp)

the " alert(resp) " runs before " resp = response " does
and thats why resp has no value


you have too do what you want to do with the response in the onSuccess function
function GetData() // step 1
{
      var resp;
      new Ajax.Request("value.php",
             {
                              method:'get',
                              onSuccess: function(transport){
                              response = transport.responseText || "";
 
                                        resp  = response;
 
                              step2(resp) // step 2
                },
                        
                onFailure: function(){  }
              });
}
 
function step2( data ) // step 2
{
 alert( data )
}

Open in new window

0
Kiran Paul VJComputer EngineerAuthor Commented:
Hi Lakio,

>>the " alert(resp) " runs before " resp = response " does and thats why resp has no value

that doesnt seem to be the problem

alert(response); shows first , then only alert(resp);
So alert(respose) execute first, after that this line resp  = response; is executed.

I think its purely related to variable scope, since there are two different functions. Even if I declare a variable as global doesnt make much difference.
0
LakioCommented:
how does your JS look like now?
well, I will show you with this code,
'GetData' will alert before 'onSuccess' does. Just try it.

function GetData()
{
      var resp;
      new Ajax.Request("value.php",
             {
                              method:'get',
                              onSuccess: function(transport){
                              response = transport.responseText || "";      
                 
                             // alert(response); // 1            
                                       alert('onSuccess')
                                        resp  = response;
                },
                       
                onFailure: function(){  }
              });
alert('GetData');
}
0
The Ultimate Tool Kit for Technolgy Solution Provi

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy for valuable how-to assets including sample agreements, checklists, flowcharts, and more!

Kiran Paul VJComputer EngineerAuthor Commented:
Hi Lakio,

First of all sorry for the late reply, i was on travel for few weeks.

I tried ur code.

But this alert('onSuccess') shows first. After that alert('GetData'); is showed.

Kiranvj
0
Michel PlungjanIT ExpertCommented:
Yes it is a scoping and more

Read the comments


function GetData()
{
      var resp; // internally known only to getData 
      new Ajax.Request("value.php",
             {
                              method:'get',
                              onSuccess: function(transport){
                              response = transport.responseText || "";      
                  
                              alert(response); // 1            
                                       
                                        resp  = response; /* no var, so global window.resp - if var here, only known to anonymous function HOWEVER there is something called closure that might get in the way here */
                },
                        
                onFailure: function(){  }
              });
alert(resp); //  how can the resp have a value here, the ajax is asyncronous and the anonymous function has not yet been called
}

Open in new window

0
Kiran Paul VJComputer EngineerAuthor Commented:
hi mplungjan,

thank you for replying.

i didnt understand these fully
/* ......... HOWEVER there is something called closure that might get in the way here */

can u plz tell abt closure or some links will be really helpful.

and

alert(resp); //  how can the resp have a value here, the ajax is asyncronous and the anonymous function has not yet been called

the ajax onSucess is already called before alert(resp); because alert(response); showed before alert(resp);

I tried the below code also but still its not working.

thanx once again
kiranvj
var resp; // made it global
function GetData()
{
      //var resp; // internally known only to getData 
      new Ajax.Request("value.php",
             {
                              method:'get',
                              onSuccess: function(transport){
                              response = transport.responseText || "";      
                  
                              alert(response); // 1            
                                       
                                        resp  = response; /* no var, so global window.resp - if var here, only known to anonymous function HOWEVER there is something called closure that might get in the way here */
                },
                        
                onFailure: function(){  }
              });
alert(resp); //  how can the resp have a value here, the ajax is asyncronous and the anonymous function has not yet been called
}

Open in new window

0
Michel PlungjanIT ExpertCommented:
http://blog.morrisjohns.com/javascript_closures_for_dummies

But try this:
var resp; // made it global
var tId;
function GetData()
{
      
      new Ajax.Request("value.php",
             {
                              method:'get',
                              onSuccess: function(transport){
                              response = transport.responseText || "";      
                  
                              alert(response); // 1            
                                       
                                        resp  = response; /* no var, so global window.resp */
                },
                        
                onFailure: function(){  }
              });
  tId = setInterval('testResp()',100);
}
function testResp() {
  if (resp) {
    alert(resp);
    clearInterval(tId)
  }
}

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Kiran Paul VJComputer EngineerAuthor Commented:
Thank you mplungjan,

The link and code was really helpful. And thanks for responding to the mail. You are really helpful.


@Lakio

Thank you for your time and efforts.

Kiranvj
0
Michel PlungjanIT ExpertCommented:
You are welcome
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JavaScript

From novice to tech pro — start learning today.