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
source to share
No one has answered this question yet
Check out similar questions: