How do I gain access to variables that come from a ajax request that is returned from a function? I can console.out the variable, but I can't seem to store it outside the scope.
How it looks like:
In the html:
function my_ajax() {
var stuff = 'stuff'; return $.ajax({ type: "POST", url: "file.php", data: {"something": "wrong"} }); /* end ajax */
} /* end my_ajax() */
...then in the js-file:
my_ajax().then(function(response){
var elements = jQuery.parseJSON(response);
var one = elements.one;
var two = elements.two;
//now say that I need to use 'one' outside this function. I try this:
stuff(elements);
});
//this works
function stuff(el) {
var something = el.one;
console.log(something);
}
//this doesn't work
function stuff(el) {
var something = el.one;
return something;
}
//this works
var try_obj = {
please_work: function(el) {
var something = el.one;
console.log(something);
}
};
So how do I store variables from ajax of this nature so that I can reuse them on the html-page or in the js-file? I need to send them back with another ajax request.