社区所有版块导航
Python
python开源   Django   Python   DjangoApp   pycharm  
DATA
docker   Elasticsearch  
aigc
aigc   chatgpt  
WEB开发
linux   MongoDB   Redis   DATABASE   NGINX   其他Web框架   web工具   zookeeper   tornado   NoSql   Bootstrap   js   peewee   Git   bottle   IE   MQ   Jquery  
机器学习
机器学习算法  
Python88.com
反馈   公告   社区推广  
产品
短视频  
印度
印度  
Py学习  »  Jquery

在jQuery中,如何在每次之后执行代码

Jeppe Donslund • 4 年前 • 1744 次点击  

我需要在jQuery之后执行som代码。每个

我试着在每次循环后编写它,但它会立即执行。 我试过了。承诺。完成,但这给了我一个错误,promise()不是函数。

我的密码

$("#loader").show();
            var url = $(this).attr("action");
            var formData = new FormData($(this)[0]);
            $(this).hide(); 
            $.ajax({
                url: "generateJson.php",
                type: 'POST',
                data: formData,
                async: true,
                cache: false,
                contentType: false,
                enctype: 'multipart/form-data',
                processData: false,
                success: function (data, status)
                {
                    $('#json').append(data); //content loads here

                },
                error: function (xhr, desc, err)
                {
                    console.log("error");
                }
            }).done(function( data ) {
                data = JSON.parse( $("#json").val() );
                //console.log(data.length);
                $.each( data, function( key, val ) {
                    console.log(val);
                    //$("#result").append(val);
                    $.ajax({
                        type: "POST",
                        url: "/generateInvoice.php",
                        dataType: "html",
                        data: val,
                        success: function (data, status) {
                            //console.log(data);
                            if (data) {
                                $("#result").append(data)
                            //  console.log("Hurray");
                            } else {
                                console.log("Error");
                            }
                        },
                        error: function (xhr, desc, err)
                        {
                            console.log("error: " + desc + " " + err);
                        }
                    });
                }).promise().done(function(){
                    $("#loader").hide();
                    $("#done").show();
                });
                
                
            });
Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/127993
文章 [ 1 ]  |  最新文章 4 年前
Kinglish
Reply   •   1 楼
Kinglish    4 年前

你正在使用 .success 和 done ,所以我删除了一个。两者都不需要。此外,为了让阅读/故障排除更容易,我将post ajax代码移到了它自己的函数中。我不知道你为什么给一个部门写信,然后又把它读回来,所以我留下了,但把它注释掉了。最后,我添加了一个简单的计数器来迭代后续的ajax调用,这样它就可以检测到什么时候一切都完成了。我偷的 idea from this post .

$("#loader").show();
var url = $(this).attr("action");
var formData = new FormData($(this)[0]);
$(this).hide();
$.ajax({
    url: "generateJson.php",
    type: 'POST',
    data: formData,
    async: true,
    cache: false,
    contentType: false,
    enctype: 'multipart/form-data',
    processData: false,
    error: function(xhr, desc, err) {
      console.log("error");
    }
  }).done(function(data) {
    $('#json').append(data); //content loads here
    runPostAjax(data)
  }),
  error: function(xhr, desc, err) {
    console.log("error: " + desc + " " + err);
  }
});



function runPostAjax(data) {
  //data = JSON.parse($("#json").val());
  let done = data.length; // number of total requests
  $.each(data, function(key, val) {
        $.ajax({
            type: "POST",
            url: "/generateInvoice.php",
            dataType: "html",
            data: val,
            success: function(data, status) {
              if (data) {
                $("#result").append(data)
                done -= 1;
                if (done == 0) {
                  $("#loader").hide();
                  $("#done").show();
                }
              } else {
                console.log("Error");
              }
            })


        }