簡體   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