<分区>
我正在尝试存储使用 $.post()
获得的值以供使用,但我遇到了变量设置之前 $.post()
正在运行。我不明白。 $.post()
被封装在一个方法中以供泛型重用。下面的 Javascript 代码。
// 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);
});
}
正如我上面所说,zip_check
将存储“undefined”,打印到控制台,然后 $.post()
将运行,但不将值返回给 zip_check
。这个问题有意义吗?