Returning value from AJAX call
Basically, im trying to write a script that uses AJAX to verify if a username exists. Depending on the response from the AJAX call, it should return true or false. I know I can accomplish this by doing async:false, but thats a no no.
Heres a basic version of the script:
-
function whatever (username) {
var result = true;
$.get( "/account/check_username/" + username, function ( response ) {
if($.trim( response ) == 'Exists') {
result = false;
}
} );
return result;
}
Anyway to have it set result to false? I know since its Async, the ajax call, the function returns the result variable before the ajax call is complete.
A friend of mine gave me a hint saying I could use a deferred promise, but I havent been able to figure it out.