Dan Caragea schrieb:
> Hi,
>
> I have some problems setting a global variable from within the success() 
> function inside a .post.
> Here is a simplified version of what I have:
>
> function addedit() {
>    var lk_id=0;
>    $.ajax({url:'ajax/my_script.php',
>                type:'POST',
>                dataType:'html',
>                data:'optype=add',
>                success:function(data) {
>                           if (data!=null && data!='') {
>                            //alert(lk_id);
>                              lk_id=parseInt(data);
>                           }
>                         }
>                });
>     alert(lk_id);
> }
>   
The problem is not the scope. $.ajax is asynchronous and therefore the 
methods returns, calls the alters and then some (undefined) time later 
the success callback is executed. While it is possible to use $.ajax for 
synchronous calls, I (and I guess everyone else on this list) recommend 
to work with the callbacks, putting your altert inside the success 
callback or calling another function from there.

-- 
Jörn Zaefferer

http://bassistance.de


_______________________________________________
jQuery mailing list
discuss@jquery.com
http://jquery.com/discuss/

Reply via email to