在我的代码,我成功地使用这种结构:写作JSON对象JSON文件
$.getJSON('Json_users_templates/SO_example.json', function(data) {
});
获得从本地上传.json文件JSON数据对象。
现在我需要一些代码来改变的对象写入到另一个地方以.json文件..
我试图用这样的代码:
$.ajax
({
type: "GET",
dataType : 'json',
async: false,
url: 'http://your.host/save_json.php',
data: { data: JSON.stringify(eventsholded) },
success: function() {alert("Thanks!"); },
failure: function() {alert("Error!");}
});
和.PHP-reciever
<?php
$myFile = "general.json";
$fh = fopen($myFile, 'w') or die("can't open file");
$stringData = $_GET["data"];
fwrite($fh, $stringData);
fclose($fh)
?>
但它不工作,Firefox显示.php文件中的错误。
我只需要写json对象如
var jsondata = {
name : "bobby",
age: "77"
};
to .json file ..我可以做到这一点,而无需使用PHP?我应该添加任何东西output.json,使其接受新的数据?
你不能在所有的浏览器到磁盘的客户机上随便写。 – 2014-08-27 16:40:13
对于本地读/写,您可能需要查看HTML5 Filereader和Filewriter API – 2014-08-27 16:44:32
PHP文件中的错误是什么? Javascript看起来很好,问题在于PHP。 – Barmar 2014-08-27 16:44:46