-2
 <script>
function getImgData(chartContainer) {
    var chartArea = chartContainer.getElementsByTagName('svg')[0].parentNode;
    var svg = chartArea.innerHTML;
    var doc = chartContainer.ownerDocument;
    var canvas = doc.createElement('canvas');
    canvas.setAttribute('width', chartArea.offsetWidth);
    canvas.setAttribute('height', chartArea.offsetHeight);
    canvas.setAttribute(
        'style',
        'position: absolute; ' +
        'top: ' + (-chartArea.offsetHeight * 2) + 'px;' +
        'left: ' + (-chartArea.offsetWidth * 2) + 'px;');
    doc.body.appendChild(canvas);

    canvg(canvas, svg);
    var imgData = canvas.toDataURL('image/png');
    canvas.parentNode.removeChild(canvas);
    return imgData;
}

function saveAsImg(chartContainer) {
    var imgData = getImgData(chartContainer);

    // Replacing the mime-type will force the browser to trigger a download
    // rather than displaying the image in the browser window.
    // window.location = imgData.replace('image/png', 'image/octet-stream');
}

function toImg(chartContainer) { 
    var doc = chartContainer.ownerDocument;
    var img = doc.createElement('img');
    img.src = getImgData(chartContainer);
    var test = getImgData(chartContainer);
    $.ajax({
        type: 'POST',
        url: "test.php",
        data: "test" + test,
        success: function (data) {
        console.log(test);
        }
    });
}

google.load('visualization', '1', {packages:['corechart', 'treemap', 'geochart']});
google.setOnLoadCallback(drawChart);

function drawChart() {
// Pie chart
    var data = new google.visualization.DataTable();
    data.addColumn('string', 'Task');
    data.addColumn('number', 'Hours per Day');
    data.addRows(5);
    data.setValue(0, 0, 'Work');
    data.setValue(0, 1, 11);
    data.setValue(1, 0, 'Eat');
    data.setValue(1, 1, 2);
    data.setValue(2, 0, 'Commute');
    data.setValue(2, 1, 2);
    data.setValue(3, 0, 'Watch TV');
    data.setValue(3, 1, 2);
    data.setValue(4, 0, 'Sleep');
    data.setValue(4, 1, 7);

    var chart = new google.visualization.PieChart(document.getElementById('pie_div'));
      chart.draw(data, {width: 450, height: 300, title: 'My Daily Activities'});
    toImg(document.getElementById('pie_div'));
}


</script>

此代码在我的“test.php”文件中。

我正在尝试使用 ajax 将变量传递给 php,如下所示:

$.ajax({
            type: 'POST',
            url: "test.php",
            data: "test" + test,
            success: function (data) {
            console.log(test);
            }
        });

当我尝试转储输出时,它返回我null(我将输出传递并转储到同一个文件中,还尝试将其传递到另一个文件并将其转储到那里 - 结果相同):

var_dump($_POST['test']);

为什么价值不传递?

4

2 回答 2

3

选项的格式data不正确。

改变

data: "test" + test,

data: {test: test},
于 2013-05-19T15:30:42.150 回答
0

此外,要拥有真正有效的 JSON,我会扩展 techfoobar 的答案:

改变

data: "test" + test,

data: {"test":"test"}
于 2013-05-19T15:33:45.430 回答