I'm trying to store a value I get with $.post()
for use, but I'm running into an issue where the variable is being set before $.post()
is being run. I don't get it. $.post()
is encapsulated in a method for generic reuse. Javascript code below.
// call the post function
var zip_check = sendPost('{some url here}', new_zip);
console.log(zip_check);
/**
* sendPost
*
* Sends data via jQuery post and returns required result.
*
* @param string target_url - where post is going
* @param string post_data - information to be sent
* @return string - data to be manipulated
*/
function sendPost(target_url, post_data) {
$.post(target_url, {
post_data:post_data
}).done(function(data) {
console.log(data);
return data;
}).fail(function(e) {
console.log('AJAX Failure: ' + e);
});
}
As I said above, zip_check
will store "undefined", print out to console, and then $.post()
will run, but not return the value to zip_check
. Does the the problem make sense?