AJAX means asynchrony. This means that in your source code saveRecord will be executed before , the client will receive a response from the server (and, depending on the implementation of $.ajax , it may be before the client sends a request to the server).
Also, you seem to misunderstand how functions work in JS. var category = autoSelectCategory(payee); sets the category to the return value autoSelectCategory ; but the autoSelectCategory function in your code returns nothing.
On the other hand, the data return value of your anonymous function can only be used by the $.ajax function (and $.ajax most likely ignores the return value of the success parameter).
Here is the code that should work:
function parseRow(row){ var rowArray = row.trim().split(","); var date = rowArray[0]; var checknum = rowArray[1]; var payee = rowArray[2]; var memo = rowArray[3]; var amount = rowArray[4]; autoSelectCategory(payee, function (category) { saveRecord(date, checkNum, payee, memo, category, payment, deposit); }); } function autoSelectCategory(payee, callback) { $.ajax({ async: false, url: "autoselectcategory", dataType: "json", data: { string: payee }, success: callback }); }
penartur
source share