【问题标题】:Jquery form submit and ajax call overlapping. Ajax call canceledJquery 表单提交和 ajax 调用重叠。 Ajax 调用已取消
【发布时间】:2012-07-30 20:45:52
【问题描述】:

我有一个网络应用程序,用户可以在其中创建发票。该应用程序基于 jquery 和 ajax 调用。引入问题的流程:

  1. 用户填写发票表格。勾选复选框以创建 PDF。
  2. 用户点击“创建发票”按钮
  3. 应用程序通过 AJAX 调用将发票数据插入数据库
  4. 如果调用成功返回,将使用发票数据创建一个 FORM,它会自动提交,然后从 DOM 中删除。在此步骤中,发票将即时创建,浏览器将开始下载 PDF。
  5. 与此同时,最后一次 AJAX 调用 (getDeliveries()...) 会删除发票表单并重新加载起始屏幕。

问题是最后一个 ajax 调用被取消(可以在 Chrome 错误控制台中看到)并且屏幕将是空白的。没有加载结果,这太令人沮丧了。我认为问题在于表单提交和ajax调用重叠。

你知道如何解决这个问题吗?任何其他强制下载文件的解决方案(我可以在哪里等待开始调用 getDeliveries 函数)?

这是我创建的js代码:

        var str = $("#invoiceForm").serialize();

    $('#invoiceForm input[disabled], #invoiceForm select[disabled]').each( function() {
        str = str + '&' + this.id + '=' + $(this).val();
    });
    //Save or update the invoice first
    $.ajax({
        type: "POST",
        url: BASEURL+"productFuncs/setInvoice.php",
        data: "f="+f+"&print=1&"+str,
        success: function(data, textStatus){
            $("#addAjax").hide();
            $("#addButton").show();

            if ( !isNaN( parseInt(data) ) ) {

                //Print out                 
                if ( $("#pdf").is(":checked") ) {
                    //Print out to PDF      
                    var url = BASEURL+"productFuncs/getPrintOut.php";       

                    var inputs = '<input type="hidden" name="f" value="'+ f +'" />' +
                                '<input type="hidden" name="pdf" value="1" />' +
                                '<input type="hidden" name="copy" value="2" />' +
                                '<input type="hidden" name="orig_id" value="'+ data +'" />';

                    $('#invoiceForm input[disabled], #invoiceForm select[disabled]').each( function() {
                        inputs+='<input type="hidden" name="'+ this.id +'" value="'+ $(this).val() +'" />';
                    });                 

                    var pairs = $("#invoiceForm").serializeArray();
                    $.each( pairs, function(i, field) { 
                        inputs+='<input type="hidden" name="'+ field.name +'" value="'+ field.value +'" />'; 
                    });

                    //send request
                    $('<form action="'+ url +'" method="post">'+inputs+'</form>').appendTo('body').submit().remove();

                }
                else {              

                    //Print out to HTML and directly to printer
                    $.ajax({
                        type: "POST",
                        url: BASEURL+"productFuncs/getPrintOut.php",
                        data: "f="+f+"&copy=2&orig_id="+data+"&"+str,
                        dataType: "html",
                        success: function(data, textStatus){
                            $("#printOut").html(data);
                            //Delay because of the IE
                            var t=setTimeout(' $("#printedArea").print() ', 3000);                          
                        }
                    });
                }

                $('#newDeliveryNote, #leftSide, #rightSide').toggle();
                getDeliveries(f, '');                   
            }
            else {
                //Not enough quantity - we have to set the ID nothing to save the delivery again
                $('#invoiceForm input[name=id]').val("");
                alert( data );
            }


        }
    });

getDeliveries 函数中的 AJAX 调用:

        $.ajax({
        type: "GET",
        url: BASEURL+"productFuncs/getDeliveries.php",
        data: "d="+d+"&f="+f,
        success: function(data, textStatus){
            $("#rightSide").html(data);

            $("#cover").hide();     
            $("#ajaxMessage").fadeOut(200);
            jsLink();       
        }
    });

【问题讨论】:

  • 为什么需要动态创建此表单?您不能在新窗口中发送此请求或创建用户单击(或使用 jquery 自动触发)以下载 pdf 的链接吗?
  • 因为我没有将 PDF 保存到服务器。我可以强制浏览器以这种方式下载PDF。谷歌了很多,但没有找到其他解决方案。该解决方案来自 Filament Groups 的网站。

标签: jquery ajax forms submit


【解决方案1】:

我认为您的表单提交是问题所在。您可以使用 url 创建一个链接以访问创建 pdf 的资源,触发点击并将其删除。

类似:

// of course, you will change href to the url that will be used to generate the pdf
$( "body" ).append( "<a id='downloadLink' href='www.stackoverflow.com' target='_blank'></a>" );
$( "#downloadLink" )[0].click();
$( "#downloadLink" ).remove();

jsFiddle:http://jsfiddle.net/davidbuzatto/QGs5n/

编辑:要使用 POST,我认为 this 会帮助您。我在这里找到它:Download File Using Javascript/jQuery

【讨论】:

  • 看起来很有希望!但由于表单中的数据很多,我需要 POST 方法。您对 POST 方法有任何想法吗?
  • 我想我找到了一个可以完成这项工作的 jQuery 插件。我发布了我的答案。
  • 谢谢大卫!该插件使用与我编写的方法相同的方法(在某些特定情况下还有另一种 iframe 方法)。但!也许我在插件的源代码中找到了一些东西。我可以设置一个 setTimeout 并观看下载。如果文件下载完成,我可以触发 getDeliveries。我会试一试,然后返回结果。
  • 嗨塔马斯。观察文件下载,也许你可以使用这里讨论的内容:stackoverflow.com/questions/2343418/… 问题不同,但我认为你可以使用一些想法来完成你的任务。
  • 嗨,大卫。这种方法:geekswithblogs.net/GruffCode/archive/2010/10/28/… 运行良好。我将它应用于我的应用程序并移植到 PHP。我给你重点是因为你给我指明了正确的方向。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-07-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多