I'm trying to get data from a php using jQuery's $.ajax
method.
This is my code right now:
var ajaxresult = null;
function myFunc() {
$.ajax('ajaxtest.php',{
type: 'get',
data: '',
success: function(data) {
ajaxresult = data;
}
});
console.log(ajaxresult);
}
$('button').click(function(){
myFunc();
})
My problem is this:
The first time I call myFunc()
(when I click the button) it logs null
to the console, after that if I click again it returns the expected value.
What could cause this and how could I fix it to return the expected value the first time it's called?