javascript - 如何为 JQuery/AJAX 'get' 例程的结果设置回调

标签 javascript jquery ajax callback jquery-callback

我已经使用我能想到的所有术语排列阅读并重新阅读了关于 JQuery/AJAX 回调的每个首页 Google 结果,并且我尝试对下面的代码进行的重写都没有成功。

我只需要为此函数构造一个回调(它是一个更大的自调用 JQuery 函数的一部分),以便“message”变量在继续执行“的评估例程之前保存integrity_check.php 例程的结果”消息'在最后。

(是的,这是使 JQuery 同步的又一次尝试,我知道回调就是答案,但我找不到它。)如果您能帮助我,祝您成功和幸福:

// 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 的建议指出了正确的方向,尽管我必须进行修改才能使其发挥作用;请参阅以下代码解决方案中的 CAPS 注释:

var request = $.get('integrity_check.php', { add_new_variable: $('#new_variable').val() }, function(data) {
    if (data != 0) {
        message = data;
    } 
    return message;
});

// HERE I HAD TO SAVE THIS VALUE TO A NEW VARIABLE; 
// $('#new_variable').val(); WAS NOT ACCESSIBLE OTHERWISE IN THE ROUTINE THAT FOLLOWED:
var nv = $('#new_variable').val();

// HERE IT WAS IRRELEVANT WHAT ARGUMENT WENT INTO function(),
// EXCEPT IT COULD *NOT* BE message; YOU HAD SUGGESTED msg, WHICH WAS IMMATERIAL, IT TURNED OUT
request.then(function() {

    // HERE I *HAD* TO USE message, NOT THE GENERIC msg THAT YOU HAD PASSED INTO THE FUNCTION:
    if (message != '') {
        alert(message);
    } else {

        // THE ORIGINAL FORM HERE WOULDN'T WORK, AS $('#new_variable').val() WAS INACCESSIBLE IN THE FUNCTION:
        //var newKeyword = '<label><input name="new_variable[]" type="checkbox" tabindex="-1" value="' + $('#new_variable').val() + '" checked /> ' + $('#new_variable').val() + '</label>';

        // THIS, HOWEVER, WORKED...USING nv IN PLACE OF $('#new_variable').val();
        var newVariable = '<label><input name="new_variable[]" type="checkbox" tabindex="-1" value="' + nv + '" checked /> ' + nv + '</label>';

        $('#checkboxes').append(newVariable);
    }
});

我很感谢 guest271314 所发布的内容,尽管我不清楚为什么我必须进行这些更改才能使代码正常工作。解释一下,有人吗?

最佳答案

尝试使用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)
});

关于javascript - 如何为 JQuery/AJAX 'get' 例程的结果设置回调,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32596032/

相关文章:

java - Ajax servlet 发送-接收不起作用

javascript - JSON 对象被 Javascript 重新排序

php - ajaxCRUD : two questions about insert

javascript - AngularJS ng-app 声明的问题

php - 编写可以像普通浏览器一样与 AJAX 网站交互的 PHP 脚本需要哪些功能?

javascript - AJAX 加载和页面滚动

javascript - 将 div 中的 Fetched 数据从 excel 转换为查询

javascript - 如何使用 Jest/Enzyme 测试 React 中文件类型输入的更改处理程序?

javascript - 通过拖动调整 pre 元素的大小

javascript - 在主干js中的 View 中嵌套 View