your code is reasonable, but you use the callback function in isValid (), so it doesn’t warn you !, I also slightly modified your ajax function below
function isValid() { checkOnServer(function(response) { alert(response); }); } function checkOnServer(callback) { $.ajax({ url : URL, data : data, success : function(response) { callback(response); } }); }
Edit
A variable cannot return from an asynchronous method? If we call a function with ajax, the call function is started first immediately before the ajax response, so it returns undefined only using the method described above.
There is two method I've know to return from asynchronous function
Call after ajax function execution
function isValid() { var data; checkOnServer(function(response) { data = response; }); return data; } function checkOnServer(callback) { $.ajax({ url : URL, data : data, async : false }).done(function(response){ callback(response); }); } console.log(isValid());
Assign a variable using responseText
function isValid() { var data = checkOnServer(); return data; } function checkOnServer() { var data = $.ajax({ url : URL, data : data, async : false
source share