I got a Jquery ajax call that gets a json response from a php file i got, the json response is great and I can console log the response correctly, however I don't seem to be able to save the information outside the ajax result, it wont update the array. the code looks like this and I posted the result below.
window.array={name: 'john',color:'red'};
console.log(array['name']);
console.log(array['color']);
$.ajax({
url : 'getJsons.php',
type : 'POST',
data : data,
dataType : 'json',
success : function (data) {
array['name'] = data['name'];
array['color'] = data['color'];
console.log(array['name']);
console.log(array['color']);
}
});
console.log(array['name']);
console.log(array['color']);
this will result in the following console:
john
red
john
red
marry
blue
So I get the console right the first time but it seem to load the script after the ajax call before the ajax call, why is that? because that makes it impossible for me to use the ajax result in the rest of the code since it's being fetched after the script is already loaded. Is there a way to get the ajax to be run before the rest?