weixin_33735077 2015-04-26 21:13 采纳率: 0%
浏览 71

用PHP创建一个json文件

I want to update the json file by overwriting the old one. I can export the data to a json string by using some jquery. And the data is stored in the variable json_update . But I don't know how to send the data to php.

   $(function () {
    $('#switcher').click(function () {
    var json_update = JSON.stringify($('#table-hover').bootstrapTable('getData'));
    $.ajax({
             type: "POST",
             url: "adding2.php",
             data: json_update,
             contentType: "application/json; charset=utf-8"
             dataType: "json",
             success: function (data) {
                alert("success");

             }
         });
    });
});     

And here is the adding2.php . Thanks for helping me.

<?php
$data = $_POST['json_update'];
$fileHandler = fopen('work2.json', 'w+');
fwrite('work2.json',$data);
fclose($fileHandler);
?>
  • 写回答

2条回答 默认 最新

  • weixin_33712987 2015-04-26 21:17
    关注

    The data you send from the client side (the value of json_update) is stored in $_POST, so just change $data = $_POST['json_update']; to $data = $_POST;.

    评论

报告相同问题?