Get variables after ajax finishes
I'm trying to get at variables returned from an ajax. Found out that those variables aren't available outside the function, so I wrote those variables to data attributes & then trying to call those later.
- function myajaxcall() {
- var v = new Array();
- $.ajax({
- url: "pathto/check.php",
- dataType: "json"
- }).done (function(check) {
- v = check;
- console.log('v Inside: ' , v); // Getting correct values, BUT it's running twice, for some reason?
- $('#mycheck').attr({'data-uflag': v.uflagexists, 'data-cflag': v.cflagexists});
- });
- }
- myajaxcall();
- $.when(myajaxcall()).done(function() { // USED WHEN DONE TO INSURE DATA VARIABLES ARE AVAILABLE
- console.log('Done running');
- var uflagcheck = $('#mycheck').attr('data-uflag');
- console.log('Uflagcheck: ' + uflagcheck); // GETTING DEFAULT VALUE OF '#' FOR DATA
- if (uflagcheck == true) {
- console.log('Uflagcheck is True');
- ...
- }
- });
This is the order I want things to run:
- ajax call (done assigns the data attributes)
- other code runs after ajax & uflagcheck picks up data value to use in later conditionals
Attached a screenshot of the v values, which are correct.
How do I get this stuff to run in proper order & pull the values correctly?