I have a scenario where I am attempting to assign a variable from an AJAX call that triggers a function. The code snippet below successfully logs imgurlthumbvar in the console, but when I try to display it using an alert, it shows up as undefined. After doing some research, I discovered that this issue is likely related to the asynchronous nature of AJAX. Can anyone provide assistance with this? Thank you in advance!
function fetchImage(id){
$.ajax({
url:'getphotos.php',
type:'POST',
dataType: "JSON",
data:{id:id},
success: function(result){
$.each(result, function(){
imgurl = this.imgurl;
imgurlthumb = this.imgurlthumb;
console.log(imgurlthumb)
return imgurlthumb
})
}
});
}
$('#test123').click(function(){
var test = fetchImage(7)
alert(test)
})