Javascript 使用画布将数据发送到服务器

Javascript 使用画布将数据发送到服务器,javascript,html,canvas,kineticjs,todataurl,Javascript,Html,Canvas,Kineticjs,Todataurl,我用canvas KineticJS制作了一些物体, 我试过了 canvas.toDataURLimage/png; 但我不知道如何获取参数值,当我点击提交按钮时,我想向服务器发送一些参数 谢谢 //根据技术,在函数、方法或模型中接受数据服务器端 例如在php中:-echo$_POST[yourData] 动能.Stage上的所有对象都可以序列化为JSON字符串: 首先,将stage序列化为JSON: var json = stage.toJSON(); 然后,可以使用jquery的ajax方

我用canvas KineticJS制作了一些物体, 我试过了 canvas.toDataURLimage/png; 但我不知道如何获取参数值,当我点击提交按钮时,我想向服务器发送一些参数

谢谢

//根据技术,在函数、方法或模型中接受数据服务器端
例如在php中:-echo$_POST[yourData]

动能.Stage上的所有对象都可以序列化为JSON字符串:

首先,将stage序列化为JSON:

var json = stage.toJSON();
然后,可以使用jquery的ajax方法将json字符串发送到服务器:

$.ajax({
  type: "POST",
  url: "http://yourSite.com/saveTheStage.php",
  data: {stageJSON: stage.toJSON()}
})
.done(function(respond){alert("done: "+respond);})
.fail(function(respond){alert("fail");})
.always(function(respond){alert("always");})
在服务器上,读取接收到的json并将其保存为唯一的文件名。本例使用PHP

<?php 

if ( isset($_POST["stageJSON"]) && !empty($_POST["stageJSON"]) ) { 

    // get the stage data
    $json = $_POST['stageJSON'];

    // create a filename for the new image
    $file = md5(uniqid()) . '.json';

    // decode the image data and save it to file
    file_put_contents($file, $json);

    // return the filename
    echo $file;

}

?>

谢谢,但是我怎样才能得到服务器端的数据呢?例如,我有一个名为LIGHT的变量,它可以是true或false,如何读取服务器端的值
<?php 

if ( isset($_POST["stageJSON"]) && !empty($_POST["stageJSON"]) ) { 

    // get the stage data
    $json = $_POST['stageJSON'];

    // create a filename for the new image
    $file = md5(uniqid()) . '.json';

    // decode the image data and save it to file
    file_put_contents($file, $json);

    // return the filename
    echo $file;

}

?>
data: {
    stageJSON: stage.toJSON(),
    LIGHT: LIGHT
}