javascript 如何使用jquery打印 AJAX 响应

oyxsuwqo  于 2022-10-30  发布在  Java
关注(0)|答案(2)|浏览(216)

我必须在for循环中调用 AJAX 。ajax给了我一个text/html响应。现在我必须打印每个响应。我正在做下面的代码:

function printBill(printBills, lastBillNo, type,taxType,outletId,date){

var printableObjects=[];
printBills.forEach(function(data){
    lastBillNo++;
    console.log(JSON.stringify(data));
    $.ajax({
        url: contextPath+"/print/"+lastBillNo+"/"+type+"/"+taxType+"/"+outletId+"/"+date,
        type: "POST",
        data: JSON.stringify(data),
        contentType: 'application/json',
        async:false,
        success:function(response){

            printableObjects.push(response);  // pushing response to array.

        }
    });
});
 return printableObjects;
}

这个函数给了我printableObjects作为数组。现在我想打印这个对象数组中的对象。为了打印这个对象,我使用了下面的代码:

function printIt(printableObj){
var counter = 0;
 var timer = setInterval(function(){
     printContent(printableObj[counter]);
    counter++;
    if (counter === printableObj.length) {
        console.log("clearing timer :"+counter+" length of the obj          :"+printableObj.length);
          clearInterval(timer);
         alert("Printing done ..");
          window.location.href="takeAway";

    }
},500);
console.log("============");
 return true;
}

function printContent(printDoc){
//console.log("Printing object is :"+el);
  var restorepage = document.body.innerHTML;
  var printcontent = printDoc;
  document.body.innerHTML = printcontent;
  window.print();
  document.body.innerHTML = restorepage;
}

我发送此打印到sendtoOne注意。每当打印发生时,我的浏览器被击中。请让我知道任何解决方案。我没有运气。

sczxawaw

sczxawaw1#

尝试在调用成功后移动foreach循环。

function printBill(printBills, lastBillNo, type,taxType,outletId,date){

 var printableObjects=[];
 printBills.forEach(function(data){
   lastBillNo++;
console.log(JSON.stringify(data));
 $.ajax({
    url: contextPath+"/print/"+lastBillNo+"/"+type+"/"+taxType+"/"+outletId+"/"+date,
    type: "POST",
    data: JSON.stringify(data),
    contentType: 'application/json',
    async:false,
    success:function(response){

        printableObjects.push(response);  // pushing response to array.
        printableObj.forEach(function(d){
       // setTimeout(printContent(d),1000);
       printContent(d);
});

    }
});
 });
   return printableObjects;
}

function printContent(printDoc){
//console.log("Printing object is :"+el);
 var restorepage = document.body.innerHTML;
 var printcontent = printDoc;
 document.body.innerHTML = printcontent;
 window.print();
 document.body.innerHTML = restorepage;
}
wqnecbli

wqnecbli2#

如果这就是您的全部代码,那么您 * 显然 * 没有等待 AJAX 的所有返回。
试用此示例(抱歉,现在无法测试)

function printBill(printBills, lastBillNo, type, taxType, outletId, date, callback){
    //We create a new array, which essentially stores all the calls to ajax
    //so we can listen to all their completions.
    var requests=[],
        printableObjects = [];
    printBills.forEach(function(data){
       lastBillNo++;
        console.log(JSON.stringify(data));
        requests.push($.ajax({
            url: contextPath+"/print/"+lastBillNo+"/"+type+"/"+taxType+"/"+outletId+"/"+date,
            type: "POST",
            data: JSON.stringify(data),
            contentType: 'application/json',
            async:false,
            success:function(response){

                printableObjects.push(response);  // pushing response to array.
            }
        }));
        //We use $.when() to check for all the ajax call's completions and
        //execute a function when they're all done. the fn.apply() is being
        //used to pass each item as an individual parameter. 
        $.when.apply($, requests).then(
            callback(printableObjects);
        );
   });
   return printableObjects;
}

你可以这样使用它:

printBill(printBills, lastBillNo, type, taxType, outletId, date, function (printableObjects) {
    //Do something with that array - printableObjects
}

更多关于$.when()的信息更多关于apply()的信息

相关问题