I am building this jQuery function to get data from my backend when a button is clicked. I have been reading this answer How do I return the response from an asynchronous call? to a similar question and applied the jQuery deferred objects solution as following.
var results;
function ajax() {
var keyword = prompt("Enter keyword you wish to search for");
return $.ajax({
type: "GET",
url: "http://localhost:8080/getGeojson"+keyword,
data: {},
dataType: 'json',
success: function (data) {
return(data);
console.log(data);
}
});
}
$('#keywordSearch').click(function() {
ajax().done(function(result) {
results = result;
console.log("something went right");
}).fail(function() {
console.log("something went wrong");
});
});
However this code is still not working. It gets the results and I see them in my console log, but the results are still not stored in the global variable results. It remains undefined.
Any help would be appreciated.
Thanks!