<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'文件中。
我正在尝试将变量传递给php,使用像这样的ajax:
$.ajax({
type: 'POST',
url: "test.php",
data: "test" + test,
success: function (data) {
console.log(test);
}
});
当我尝试转储输出时,它返回null
(我传递并将输出转储到同一个文件中,也尝试将其传递给另一个文件并将其转储到那里 - 结果相同):
var_dump($_POST['test']);
为什么值不通过?
答案 0 :(得分:3)
data
选项的格式不正确。
更改
data: "test" + test,
到
data: {test: test},
答案 1 :(得分:0)
此外,为了拥有真正有效的JSON,我会扩展techfoobar的答案:
更改
data: "test" + test,
到
data: {"test":"test"}