I am making a simple JSON post to the server and I retrieve just on boolean. The problem is that this json call is inside another function which needs to return that boolean as well.
I have two solutions here
-
Somehow return data from JSON return function and then return it again from wrapper function (the code that I have here doesn’t work, I can’t figure out how to return data from “function(){} inside $.postJSON:
function testZipCode(zipcode, state) { var s; $.postJSON("/Cart/ZipTest", { state: state, zipCode: zipcode }, function (data) { if (!data.ok) { s = false; } s = true; }); return s; }}
-
Wait for POST to finish and then return the result like this: (the error I get here is that I call return before POST is finished, thus responseText is null. Can anyone please help?
var result = $.postJSON("/Cart/ZipTest", { state: state, zipCode: zipcode }, function (data) { if (!data.ok) { //do something } }); return $.parseJSON(result.responseText).ok;
Return the ajax call as a deffered object and do something in the callback function: