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 = 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)
    });
  },

      

Test.php code 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 would be helpful to me because I am very new to JavaScript.

+3
json javascript php


source to share


No one has answered this question yet

Check out similar questions:

9653
What is the correct JSON content type?
6956
Can comments be used in JSON?
4829
How do I include a JavaScript file in another JavaScript file?
3915
Why does Google add while (1); into your JSON responses?
2858
How can I print JSON in a shell script?
2480
How do I send JSON data using Curl from terminal / command line to Test Spring REST?
1817
How to customize Microsoft JSON date?
1635
Parsing JSON in JavaScript?
1542
How can I print JSON using JavaScript?
1349
Why can't Python parse this JSON data?



All Articles
Loading...
X
Show
Funny
Dev
Pics