Failed to update JSON file data with response.js

I am trying to update comment.json using one of the sample React.js sample app examples.

I made very few changes to the code:

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


Test.php code as folllows:

   $json = $_POST['json'];
   echo $json;

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


Any help would be helpful to me because I am very new to JavaScript.


source to share

All Articles