In my function, I have a condition that checks the idType. If it is 1, an ajax call is made to a php file to retrieve the name associated with the specific idName and return it. Otherwise, another example value is returned.
function obtainName(idName, idType){
var resultString = ""
if(idType == 1){
$.ajax({
type:"GET",
url:"myFile.php",
dataType:"JSON",
data:{idName: idN},
success: function(data){
resultString = data.name
console.log(resultString)
return resultString
},
error: function(d){
}
});
} else {
resultString = "otherValueName"
console.log(resultString)
return resultString
}
}
When calling this function with specific id names, get_name is utilized with the corresponding idName and type. The for loop is omitted in this example.
function displayObtainedName(exampleIdName){
var string= get_name(exampleIdName, 1);
console.log(string)
return string
}
Lastly, the name is printed out:
displayObtainedName("Alex");
However, if idType is 1 and triggers an ajax call, the result appears as undefined. In the console, the outcome of the variable "string" in the displayObtainedName function shows as undefined. But the value assigned to the variable "resultString" in get_name is correct.
What can be done to solve this issue?