我已经阅读并重新阅读了JQuery / AJAX回调中的每个首页Google结果,使用了我能想到的每个词汇排列,并且没有重新编写我已经尝试过以下代码成功的。
我只需要为这个函数构建一个回调 - 这是一个更大的自调用JQuery函数的一部分 - 所以'消息'变量保存了integrity_check.php例程的结果,然后再进入“消息”的评估例程。在末尾。
(是的,这是让JQuery同步的又一次尝试,我知道回调就是答案,但我无法找到它。)如果你可以帮助我,那么成功和幸福会降临到你身上:
Alamofire 2.0
更新
Guest271314的建议指出了正确的方向,虽然我不得不进行修改以使其工作;请参阅以下代码解决方案中的CAPS评论:
// If the integrity check passed, message will be empty.
// If the integrity check fails, message will hold text for an alert box.
var message;
$.get('integrity_check.php', { add_new_variable: $('#new_variable').val() }, function(data) {
if (data != 0) {
message = data;
}
});
[...some other code follows that may or may not set 'message' variable to a text message for alert box, so the following code must stand independently:]
if (message != '') {
alert(message);
} else {
[...proceed with using new_variable in HTML...]
}
我非常感谢guest271314发布了他/她发布的内容,虽然我不清楚为什么我必须进行更改才能使代码生效。解释,有人吗?
答案 0 :(得分:1)
尝试使用deferred.then()
// If the integrity check passed, message will be empty.
// If the integrity check fails, message will hold text for an alert box.
var message;
var request = $.get('integrity_check.php'
, { add_new_variable: $('#new_variable').val() }
, function(data) {
if (data != 0) {
message = data;
}
return message
});
/*
[...some other code follows that may or may not set 'message' variable to a text message for alert box, so the following code must stand independently:]
*/
request.then(function(msg) {
// `msg`: `message`
if (msg != '') {
alert(msg);
} else {
// [...proceed with using new_variable in HTML...]
}
// return msg
}, function err(jqxhr, textStaus, errorThrown) {
console.log(errorThrown)
});