繁体   English   中英

Ajax调用和function.apply无限循环

[英]Ajax call and function.apply infinite loop

我正尝试在为此目的而构建的Web应用程序中打印.CSV,我有一个链接,它很简单:

<a class="areaSummaryExport" value="1">...</a>

然后使用Javascript和JQuery执行:

$('.areaSummaryExport').on('click', function(){
    exportAreaSummary($(this).attr('value'), $(this));
});

要使用JQuery导出.CSV,我需要一个<table>所以我使用ajax从服务器获取它:

function exportAreaSummary(selected, sender){
    $.ajax({
        url: 'admin_ajax.php',
        method: 'get',
        dataType: 'json',
        data: {action: 'exportAreaSummary', area: selected}
    }).done(function(data){
        console.log('done');
        exportAreaSummary.apply(this, [data.table, data.name, data.header])
    }).error(function(XMLHttpRequest){
        console.log('error');
        console.log(XMLHttpRequest);
    });
}

当我取回它时,我“应用”此函数来创建和下载它:

function exportTableToCSV($table, filename, header) {
    var $rows = $table.find('tr:has(td),tr:has(th)'),

        // Temporary delimiter characters unlikely to be typed by keyboard
        // This is to avoid accidentally splitting the actual contents
        tmpColDelim = String.fromCharCode(11), // vertical tab character
        tmpRowDelim = String.fromCharCode(0), // null character

        // actual delimiter characters for CSV format
        colDelim = '","',
        rowDelim = '"\r\n"',

        // Grab text from table into CSV formatted string
        csv = '"' + 'sep=,' + rowDelim +
        (header? header.join(colDelim) + rowDelim : '') +
        $rows.map(function (i, row) {
            var $row = $(row),
                $cols = $row.find('td, th');

            return $cols.map(function (j, col) {
                var $col = $(col),
                    text = $col.text();

                return text.replace(/"/g, '""'); // escape double quotes

            }).get().join(tmpColDelim);

        }).get().join(tmpRowDelim)
            .split(tmpRowDelim).join(rowDelim)
            .split(tmpColDelim).join(colDelim) + '"',

        // Data URI
        csvData = 'data:application/csv;charset=utf-8,' + encodeURIComponent(csv);
    $(this)
        .attr({
        'download': filename,
            'href': csvData,
            'target': '_blank'
    });
}

最后一个函数可以在其他地方完美运行,但是该表已经在页面中,因此我不必使用任何ajax。 这里的表不应该在页面中,所以我用ajax来获取它,然后将其发送到exportTableToCSV() 但是然后(我不确定为什么)它会无限循环直到服务器崩溃(最后一次有超过2000次对该服务器的调用)。

有谁知道为什么会这样以及如何解决? 任何其他方式也可以为我工作。

您正在成功调用exportAreaSummary而不是exportTableToCSV

function exportAreaSummary(selected, sender){
    $.ajax({
        url: 'admin_ajax.php',
        method: 'get',
        dataType: 'json',
        data: {action: 'exportAreaSummary', area: selected}
    }).done(function(data){
        console.log('done');
        exportTableToCSV($(data.table), data.name, data.header)
    }).error(function(XMLHttpRequest){
        console.log('error');
        console.log(XMLHttpRequest);
    });
}

暂无
暂无

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

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