I'm reasonably new to using ajax and was wondering if someone could help me out with this trouble I'm having with getting the callback to actually work properly.
I've read a bunch of the questions on this board already, but none of them deal with actually assigning the data to a variable, other than a html element.
Basically, I want to do something like this:
// Variable that needs the data.
x = callAjaxFunction(someValue, anotherValue);
/* Do something with variable x. */
To fill this variable, I use ajax to call a .php
file on my webserver to talk to the database. Like so:
function getData(action, value) {
$.ajax({
url: '/some/url.php',
data: { action: action, value: value },
type: 'post',
dataType: "json"
})
.done(function(response){
returnData(response, action);
})
.fail(function(){
console.log("Error occured!");
});
}
According to the stuff I've read on the web, paired with what I'm seeing in the console in Chrome, this is working. Yet, my javascript function that calls the function still continues and treats variable x
as empty.
Anyone can enlighten me? Am I doing something wrong in the ajax call function, or lies the problem somewhere in calling this function from the right place?
Here is a jsfiddle link with some pseudo-code.