dongzhenqi2015 2014-09-27 13:57
浏览 291

无法通过react.js更新JSON文件数据

I am trying to update the comments.json by using one of tutorial example of Comment app of React.js.

I have done very few changes in code :

handleCommentSubmit: function(comment){
var comments = this.state.data;
var newComments = comments.concat([comment]);
this.setState({data: newComments});
$.ajax({
      url: 'test.php',
      dataType: 'json',
      type: 'POST',
      data: comment,
      success: function(data) {
        this.setState({data: data});
      }.bind(this),
      error: function(xhr, status, err) {
        console.error(this.props.url, status, err.toString());
      }.bind(this)
    });
  },

Code of Test.php as folllows:

<?php
   $json = $_POST['json'];
   echo $json;

   if (json_decode($json) != null) { /* sanity check */
     $file = fopen('comments.json','w+');
     fwrite($file, $json);
     fclose($file);
   } else {
     echo "error found"; 
   }
?>

Any help will be good for me, because I am very new to JavaScript.

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 素材场景中光线烘焙后灯光失效
    • ¥15 请教一下各位,为什么我这个没有实现模拟点击
    • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
    • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
    • ¥20 有关区间dp的问题求解
    • ¥15 多电路系统共用电源的串扰问题
    • ¥15 slam rangenet++配置
    • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
    • ¥15 ubuntu子系统密码忘记
    • ¥15 保护模式-系统加载-段寄存器