我想将带有图像字符串的数组发送到我的php动作。这是我到目前为止所做的:
$('#savepdf').click(function() {
var imagesarray = new Array();
var count = 4;
var quizid = <?php echo json_encode($quizid); ?>;
for (var i=0; i<count; i++)
{
var chart = Highcharts.charts[i];
var canvasname;
if(i == 0){
canvasname = "canvas";
}
else{
canvasname = "canvas" + i;
}
// get highcharts
canvg(document.getElementById(canvasname), chart.getSVG())
var canvas = document.getElementById(canvasname);
var img = canvas.toDataURL("image/png");
imagesarray[i] = img;
}
imagesarray = JSON.stringify(imagesarray);
// AJAX CALL TO ACTION
$.download('/results/savepdf','quizid=' + quizid + '&image=' + imagesarray);
});
我发送的数组如下所示:
[ “数据:图像/ PNG; BASE64,iVBORw0KGgoAAAANSUhEUgAAAlgAAAGQCAYAAAByNR6YAAAgAElEQ ... 197b1kPfJ5y3guHO4WLEyAAAECBAgcEFjz7y + zZJ6ttf8GC0YA4ro / bucAAAAASUVORK5CYII =”, “数据:图像/ PNG; BASE64,iVBORw0KGgoAAAANSUhEUgAAAlgAAAGQCAYAAAByNR6YAAAgAElEQ ... 9v2cl0YeqeBTqMAAIIIIAAAgMINO3aT3a + sfNxGIa7fwBwOTGmIk2OYgAAAABJRU5ErkJggg ==”,“数据:图像/ PNG; BASE64, iVBORw0KGgoAAAANSUhEUgAAAlgAAAGQCAYAAAByNR6YAAAgAElEQ ... xdLNBhIkAEiAARIAJEIA8I7FRlf1T / XLD / hhcoUOCv / we6Hn4A9659wwAAAABJRU5ErkJggg ==”, “数据:图像/ PNG; BASE64,iVBORw0KGgoAAAANSUhEUgAAAlgAAAGQCAYAAAByNR6YAAAgAElEQ ... BdGOlYwAkjASSABJAAEkACBgm8UI89qf6sUX9GRIkS5cr / AFfL9D3Dad7CAAAAAElFTkSuQmCC”]
在我的PHP操作中,我执行以下操作:
if(isset($_POST['quizid']))
$quizid = $_POST['quizid'];
if(isset($_POST['image']))
$image = $_POST['image'];
var_dump(json_decode($image));
转储只显示“NULL”。当我这样做时:
var_dump($image);
我得到:string(1) "["
答案 0 :(得分:2)
尝试:
imagesarray = encodeURIComponent( JSON.stringify(imagesarray) );
<强>更新强>
并在php中使用
$image = json_decode(urldecode($_POST['image']));
答案 1 :(得分:0)
我不熟悉$.download
方法。据推测,它是一个允许文件下载的AJAX扩展。我猜怎么可能有用。
问题是您没有发送有效的HTTP请求。需要转义JSON才能通过HTTP发送。你可以使用encodeURIComponent
自己做,或者你可以让jQuery的$.param
方法为你处理它。
var data = $.param({
quizid: quizid,
image: imagesarray
});
$.download('/results/savepdf', data);