I know, I am such a noob :(
function node_get_params($id){
Ext.Ajax.request({
url:'index.php?lander=console.gui.node_get_params' ,
params:'node_id=' + $id,
method:'POST',
success:function(result, request){
var data = Ext.util.JSON.decode(result.responseText);
alert(data.cat_name); //this works
},
failure:function(){
Ext.Msg.alert('Failed to contact server. Please try again');
}
});
alert(data.cat_name); //this gives a not defined What is the meaning - Error variable not defined? - WebDeveloper.com:: 3 posts - 3 authors - Last post: Sep 7, 2005What is the meaning - Error variable not defined? When I try this script I get an error saying that the variable is not defined. http://www.webdeveloper.com/forum/showthread.php?t=78300HOME |
return data;
}
how do I get that variable out of the the success function so that it can be returned by the wrapper function? Thanks for any help you can give because I am pulling my hair out!
-Lobos
There are 2 issues here:
1) You are declaring data in the anonymous method that you're passing to the success handler, as such, it isn't valid anywhere except in that function.
2) When you make an ajax call, it is asynchronous (your script will continue to process while the call is being made to the server). So even if data was valid in scope, it would be null because the request hasn't completed yet.
Ah yes I see what you mean, so what is happening is that the entire function is ran before the ajax transaction is finished, so we are getting to the the bottom before the variable has been populated (I understand the scope thing - if I take the "var" off the front this will become global, right?
Ok so I might be doing this wrong then, what I am trying to do is pre-populate some form fields. What I will do is create a new post more applicable to this.
Just out of interest, what would be the best way to do this, ie make the function wait until the ajax transaction is completed before returning variables?
Thank you very much for your help thus far guys :)
-Lobos
If you take the var off you'll just have an undefined variable. When you're using callbacks, you need to think about it a bit differently:
Ext.Ajax.request({
url:'index.php?lander=console.gui.node_get_params' ,
params:'node_id=' + $id,
method:'POST',
success:function(result, request){
var data = Ext.util.JSON.decode(result.responseText);
alert(data.cat_name); //this works
//do success processing here, your function shouldn't return a value
},
failure:function(){
Ext.Msg.alert('Failed to contact server. Please try again');
}
});
I think you need to read up on asynchronous javascript calls. Sorry cant give you a link offhand now.
Thanks for the help. I ended up using another technique to get the data I needed (it was needed for pre populating a form.
http://extjs.com/forum/showthread.php?p=86956#post86956
BUT, the this thread has given me a good insight into some AJAX things and I am sure this knowledge will help me in the future. Thanks again for your help!
-Lobos
Get Smart About Monitoring Virtual Machines
Microsoft Gets Ex-Streamly Cozy with U.K.'s MediaWave
|