I'm trying to call an API and check to see if a username exists in a database. The API works perfectly and is returning the username correctly. The issue I am having is that I need to define some variables globally to use later.
$(document).ready(function() {
var deviceID = getUrlVariables()["me"];
$.ajax({
    url: "funcEntity.cfc?method=getUsername&returnformat=json",
    dataType: "json",
    data: {
        deviceID: deviceID
    },
    success: function(result) {
        if(result == 0){
            alert("Proceed to login menu.");
        }
        else{
            username = result;
        }
    }
});
alert(username);
});
The result of the API should be the username and should be able to use globally in other functions? Correct? However, I am getting an error saying the variable "username" is undefined.
 
     
    