I have a variable in javascript which contains the structure of a json record. Is it possible to create and write it as .json? Can anyone provide me with an example? I am suspecting that I need to use php but I cannot find a similar example.
From my javascript file called canvasJS.js :
var delayInMilliseconds = 1000; //1 second
setTimeout(function() {
seq["canvases"] = allCanvases;
var jarraySeq = [];
jarraySeq.push(seq);
obj["sequences"] = jarraySeq;
}, delayInMilliseconds);
console.log(obj);
var xmlhttp = new XMLHttpRequest();
xmlhttp.open("POST", "/json.php");
xmlhttp.setRequestHeader("Content-Type", "application/json");
xmlhttp.send(JSON.stringify(obj));
This is my json.php file -
<?php
debug_to_console("test");
$json = file_get_contents('php://input');
file_put_contents('file.json', $json);
function debug_to_console($data) {
$output = $data;
if (is_array($output))
$output = implode(',', $output);
echo "<script>console.log('Debug Objects: " . $output . "' );</script>";
}
?>
but I am not able to create the file.json nor get an error in console. Any idea why?