简体   繁体   English

Ajax jquery同步回调成功

[英]Ajax jquery synchronous callback success

I have this function that makes an ajax call. 我有这个函数进行ajax调用。 I'm describing the problem in the last chunk of code comments. 我在最后一段代码注释中描述了这个问题。

    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;
    }

Based on the problem as described in the code comments, what changes are best for this situation? 根据代码注释中描述的问题,哪种更改最适合这种情况?

You need to set async: false for synchronous requests like this: 您需要为同步请求设置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;
}

see here for details 看到这里了解详细信息

Either set the Ajax call to synchronous as stefita pointed out, or just move your code into the success callback. 将scax指向的Ajax调用设置为synchronous,或者只是将代码移动到成功回调中。 Why can't you do this? 你为什么不能这样做? Even if it's another Ajax call it still can be done - you can nest them. 即使它是另一个Ajax调用它仍然可以完成 - 你可以嵌套它们。 With the information given by you so far (I can't see the problematic code, nor I have enough domain knowledge about your project) I don't see a problem, really. 到目前为止您提供的信息(我看不到有问题的代码,也没有足够的关于您的项目的领域知识)我真的没有看到问题。

I prefer to use callback to do the job because it achieves exactly the same result without actually making it synchronous. 我更喜欢使用回调来完成这项工作,因为它实现了完全相同的结果而没有实际使它同步。 I use success:callback and then pass in the callback as a parameter. 我使用成功:回调然后传入回调作为参数。

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

I then call this function like this: 然后我调用这个函数:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM