Javascript 无法通过react.js更新JSON文件数据
我正在尝试使用React.js的Comment应用程序的一个教程示例来更新comments.json 我对代码做了很少的更改:Javascript 无法通过react.js更新JSON文件数据,javascript,php,json,Javascript,Php,Json,我正在尝试使用React.js的Comment应用程序的一个教程示例来更新comments.json 我对代码做了很少的更改: handleCommentSubmit: function(comment){ var comments = this.state.data; var newComments = comments.concat([comment]); this.setState({data: newComments}); $.ajax({ url: 'test.php',
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的代码如下:
<?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";
}
?>
任何帮助都会对我有好处,因为我对JavaScript非常陌生。data:“json=“+comments我已经尝试过了,但它不能解决我的问题。基本上,我正试图在React.js Framwork和php的帮助下更新.json文件。在您的php文件中,您是否获得$json的值…如果您获得了值,请检查是否允许您对文件拥有写入权限问题是,我没有获得@json的值。。我调试代码时,它总是会得到一部分:else{echo“error found”}remove dataType:'json'