简体   繁体   English

Ajax调用和function.apply无限循环

[英]Ajax call and function.apply infinite loop

I'm trying to print a .CSV in a web app I'm building for that purpose I have a link, something simple: 我正尝试在为此目的而构建的Web应用程序中打印.CSV,我有一个链接,它很简单:

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

Then with Javascript and JQuery I do: 然后使用Javascript和JQuery执行:

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

To export the .CSV with JQuery I need a <table> so I use ajax to get it from the server: 要使用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);
    });
}

And when I get it back, I 'apply' this function to create and download it: 当我取回它时,我“应用”此函数来创建和下载它:

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'
    });
}

This last function works perfectly somewhere else, but there the table is already in the page so I don't have to use any ajax. 最后一个函数可以在其他地方完美运行,但是该表已经在页面中,因此我不必使用任何ajax。 Here the table shouldn't be in the page, so I'm getting it with ajax and then sending it to exportTableToCSV() . 这里的表不应该在页面中,所以我用ajax来获取它,然后将其发送到exportTableToCSV() But then (I'm not sure why) it goes in an infinite loop until the server crashes (last time there were 2000+ calls to the server). 但是然后(我不确定为什么)它会无限循环直到服务器崩溃(最后一次有超过2000次对该服务器的调用)。

Does anyone know why this is happening and how to fix it? 有谁知道为什么会这样以及如何解决? Any other way to do it will also work for me. 任何其他方式也可以为我工作。

You are calling exportAreaSummary in the success not exportTableToCSV 您正在成功调用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