I am using ajax to get some values from a database. In my java script file I have a global variable that some functions need to access it. I need to fill that variable from the values from the database. When I use ajax then the variable stays empty. If I use the async:false
in my ajax code I get that warning message in my browser's dev tools:
jquery-3.2.1.min.js:4 [Deprecation] Synchronous XMLHttpRequest on the main thread is deprecated because of its detrimental effects to the end user's experience. For more help, check https://xhr.spec.whatwg.org/.
Moreover using the above it doesn't fill the variable resulting to errors in the other functions. Plus, from what I read this is deprecated and should not be used in any circumstances.
my code
var array ={};
var flag = 'true';
$.ajax({
url: "name.php",
method: "post",
data:{flag:JSON.stringify(flag)},
success: function(data) {
array = JSON.parse(data);
},
async: false,
error: function(xhr, ajaxOptions, thrownError) {
console.log(thrownError);
}
});
What, basically I'm trying to do, is to fill the array variable and make it an proper array.
the data from the the database is like this
{
'something': { 'act1': 'act1a','act2': 'act2a'},
'something2': { 'act1': 'act1a','act2': 'act2a'}
}
anyone have any suggestions on how to achieve that?
PS: I need to fill a variable with the ajax response which will be accessible by other functions... not Convert a JSON Object into Javascript array...Im retrieving the data in string format