I am working with a django 2.0 template, a third party jQuery script for tagging photos, and my "glue" JavaScript code. I am a complete newbie to JavaScript and JQuery.
I can make a successful ajax call to the django template to get the data I want. However, I cannot seem to find a way to make that data available to the rest of my code, and in particular the third party tagging script.
My code:
(function($) {
$(document).ready(function(){
console.log("Made it! Image width="+$( "#img1" ).width()+", height="+$( "#img1" ).height());
// Ajax request to get the list of auto complete names from the server
// This function works correctly - all console messages in the function work
var autoCompleteNames = function() {
var tmp = null;
$.ajax({
url: '/biometric_identification/ajax/get_people_list/',
type: 'get',
dataType: 'json',
success: function (data) {
console.log('in success data='+data);
tmp = data;
console.log('tmp 1='+tmp);
console.log('tmp[0]='+tmp[0])
}
});
return tmp;
}();
// this statement returns null for autoCompleteNames
console.log('autoCompleteNames 3='+autoCompleteNames);
// Stuff for the third party tagging script
$("#img1").tag({
showTag: 'always',
canDelete: true,
canTag: true,
defaultTags: [
{'id':1,'label':'Unchanged','width':283,'height':283,'top':1020,'left':1539},
{'id':2,'label':'Scaled','width':72,'height':72,'top':208,'left':151},
],
autoComplete: autoCompleteNames,
});
});
})(jQuery);
As you can see in the comments in the code, the ajax part works, but when I try to print the list of values to the console outside of the ajax call, the value is null.
How do I get the values from the ajax script into the rest of my code? Is this an issue of scope or a timing issue on when the ajax call returns or something else?