【问题标题】:Ajax jquery synchronous callback successajax jquery同步回调成功
【发布时间】:2009-10-15 14:07:29
【问题描述】:

我有这个进行 ajax 调用的函数。我在最后一段代码 cmets 中描述了这个问题。

    function doop(){
            var that = this;
            var theold = "theold";
            var thenew = "thenew";

            $.ajax({
                    url: 'doop.php',
                    type: 'POST',
                    data: 'before=' + theold + '&after=' + thenew,
                    success: function(resp) {
                            if(resp == 1) {
                                    $(that).siblings('.theold').html(thenew);
                            }
                    }
            });

            // I have some code here (out of the ajax) that **further** changes 
            // the .theold's html beyond what it was changed inside ajax success
            // but the change depends on whether the resp (inside the success 
            // function) returned 1 or not, so this code out here depends on the ajax
            // so it looks like I have to turn this ajax call into a sync ajax

            return false;
    }

根据代码 cmets 中描述的问题,什么变化最适合这种情况?

【问题讨论】:

    标签: javascript jquery ajax synchronous


    【解决方案1】:

    你需要为这样的同步请求设置 async: false :

    function doop(){
            var that = this;
            var theold = $(this).siblings('.theold').html();
            var thenew = $(this).siblings('.thenew').val();
    
            $.ajax({
                    async: false,
                    url: 'doop.php',
                    type: 'POST',
                    data: 'before=' + theold + '&after=' + thenew,
                    success: function(resp) {
                            if(resp == 1) {
                                    $(that).siblings('.theold').html(thenew);
                            }
                    }
            });
    
            // some other code
    
            return false;
    }
    

    详情请见here

    【讨论】:

    • 我一直在读到设置async: false 不好,而callback 更好。但我怀疑事情就这么简单。根据我更新的问题,您推荐哪个?
    • 嗯,其实你为success属性声明的函数就是回调函数。顺便说一句,我刚刚看到您正在将结果与整数进行比较,但我很确定您默认获取的是文本,所以它应该是 resp == "1"
    • 顺便说一句。您可以在 ajax 请求之外的某个地方定义您的回调函数。可能你看到的正是那个——一个叫做回调的函数。
    • 其实你是对的,我在获取文本,1只是一个例子。当我在这里发布代码时,我会尝试简化我的代码,以使人们更容易通过;)
    • @stefita:我明白了,所以他可能有:success: callback 而不是 success: function(resp) 并在 ajax 之外的某个地方定义了回调。听起来不错,我想我应该在正确的轨道上回答这些问题。感谢您的帮助。
    【解决方案2】:

    要么像 stefita 指出的那样将 Ajax 调用设置为同步,要么只是将您的代码移动到成功回调中。为什么你不能这样做?即使它是另一个 Ajax 调用,它仍然可以完成 - 你可以嵌套它们。到目前为止,根据您提供的信息(我看不到有问题的代码,对您的项目也没有足够的领域知识),我真的没有看到问题。

    【讨论】:

    • 我明白了,所以 $(.ajax) 调用可以嵌套。听起来不错,我会试试看效果如何。
    【解决方案3】:

    我更喜欢使用回调来完成这项工作,因为它实现了完全相同的结果,而实际上并没有使其同步。我使用了success:callback,然后将回调作为参数传入。

     function getData(callback) {
          $.ajax({
              url: 'register/getData',
              data: "",
              dataType: 'json',
              success: callback
          });
      }
    

    然后我像这样调用这个函数:

      getData(function(data){
        console.log(data); //do something 
      });
    

    【讨论】:

      猜你喜欢
      • 2011-11-11
      • 2012-05-04
      • 1970-01-01
      • 1970-01-01
      • 2013-01-23
      • 2011-08-05
      • 1970-01-01
      • 1970-01-01
      • 2011-04-12
      相关资源
      最近更新 更多