Accessing variables outside ajax

I have this code and i realize that i cant have access outside from ajax except I globalize the data variable. Eg

$.ajax({
                url: 'includes/ajax_chart.php',
                data:{present:'present'},
                type: 'POST',
                dataType: 'JSON',
                success: function (data) {
                     if(!data.error){
                        b=data;                      
                                             
                     }   
                    }
                    
            });

So we must work with global variables and data or there is another way to keep these variables inside the function that contains Ajax?

that will fail if the AJAX call is not done.

Promises. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise

1 Like

you can use the when
https://api.jquery.com/jquery.when/
or (i don’t like this method) you can use global variable and call the ajax with async: false

1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.