You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

JQuery批量生成预订PDF函数异常,请求技术排查帮助

问题排查与修复方案

核心问题分析

单个PDF生成接口正常,但批量调用时出错,大概率是generateCommentsSheet函数的AJAX请求未正确处理二进制响应,或是批量请求的并发限制导致部分请求失败。


具体修复点

1. 指定AJAX响应类型为二进制

PDF属于二进制文件,默认AJAX会将响应解析为文本格式,导致生成的Blob对象无效。必须在AJAX配置中添加responseType: 'blob':

function generateCommentsSheet(reservationID) {
    $.ajax({
        type: "GET",
        url: "/Reservations/GenerateCommentSheetPDF",
        data: { reservationID: reservationID },
        responseType: 'blob', // 关键:明确指定响应为二进制Blob
        success: function (response) {
            var link = document.createElement("a");
            link.href = URL.createObjectURL(response); // 直接使用响应的Blob对象
            link.download = "Comments_" + reservationID + ".pdf";
            link.click();
            URL.revokeObjectURL(link.href); // 释放临时URL,避免内存泄漏
        },
        error: function (xhr, status, error) {
            console.error("生成PDF失败:", reservationID, error);
            alert("Failed to generate comments PDF for reservation ID: " + reservationID);
        }
    });
}

2. 移除重复的URL参数拼接

原代码中手动在URL拼接了reservationID参数,同时又通过data字段传递,容易造成参数冲突,建议只保留data传递参数的方式。

3. 控制批量请求的并发数

浏览器对同一域名的并发请求数通常限制为6个左右,循环直接发起大量请求会导致部分请求被阻塞或失败。可以改为串行请求逐个处理:

success: function (reservations) {
    console.log("Selected Restaurant ID:", selectedRestaurantId);
    console.log("Fetched Reservations:", reservations);

    // 串行执行请求,避免并发超限
    let currentIndex = 0;
    function processNextReservation() {
        if (currentIndex >= reservations.length) return;
        
        const reservation = reservations[currentIndex];
        console.log("Generating comments sheet for Reservation ID:", reservation.id);
        
        $.ajax({
            type: "GET",
            url: "/Reservations/GenerateCommentSheetPDF",
            data: { reservationID: reservation.id },
            responseType: 'blob',
            success: function (response) {
                var link = document.createElement("a");
                link.href = URL.createObjectURL(response);
                link.download = "Comments_" + reservation.id + ".pdf";
                link.click();
                URL.revokeObjectURL(link.href);
                
                currentIndex++;
                processNextReservation(); // 完成当前请求后处理下一个
            },
            error: function (xhr, status, error) {
                console.error("生成PDF失败:", reservation.id, error);
                alert("Failed to generate comments PDF for reservation ID: " + reservation.id);
                
                currentIndex++;
                processNextReservation(); // 即使失败,继续处理后续订单
            }
        });
    }
    processNextReservation(); // 启动串行处理
},

4. 后端接口验证

确保GenerateCommentSheetPDF接口返回正确的二进制文件流:

public IActionResult GenerateCommentSheetPDF(int reservationID)
{
    // 生成PDF字节数组的逻辑
    byte[] pdfContent = GeneratePdfContent(reservationID);
    
    // 返回PDF文件流,指定正确的MIME类型
    return File(pdfContent, "application/pdf", $"Comments_{reservationID}.pdf");
}

排查建议

  • 打开浏览器开发者工具(F12)的Network标签,查看批量请求的状态码、响应内容,确认是否有请求失败或响应格式错误。
  • 检查后端日志,确认GenerateCommentSheetPDF接口在批量调用时是否出现异常。

内容的提问来源于stack exchange,提问作者manoman

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.13 00:07:03