我想在react js
中显示数据库中的内容React JS出错了
Error: Parse Error: Line 27: Unexpected token . at http://localhost/PHP-React-Demo/ajax_call.html return {cell.data.row}
我的代码是:
的index.php
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<link rel="stylesheet" type="text/css" href="css/machineTest.css">
<head>
<title>Index</title>
</head>
<body>
<?php
$servername = "localhost";
$username = "root";
$password = "";
$db_name = "anand";
$conn = new PDO("mysql:host=$servername;dbname=$db_name",$username,$password);
$result = $conn->prepare("SELECT * FROM student");
$result->execute();
?>
<?php
foreach($result as $row){
echo "$row[0]$row[1]$row[2]$row[3]$row[4]";
}
?>
</body>
</html>
and react code is :
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Initial Data Via AJAX</title>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/0.13.3/react.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/0.13.3/JSXTransformer.js"></script>
</head>
<body>
<div id="example"></div>
<script type="text/jsx">
var ImageCollect = React.createClass({
getInitialState: function() {
return {
phpData: []
};
},
componentDidMount: function() {
var self = this;
$.get(this.props.source, function(result) {
var collection = result.data.children;
if (this.isMounted()) {
this.setState({
phpData: collection
});
}
}.bind(this));
},
render: function() {
BDdata = this.state.phpData || [];
return (
<div>
Images:
{BDdata.map(function(cell){
return {cell.data.row}// I think i want to change this line.
})}
</div>
);
}
});
React.render(
<ImageCollect source="http://localhost/PHP-React-Demo/index.php" />,
document.getElementById('example')
);
</script>
</body>
</html>
我只想在反应页
上显示从index.php中恢复的内容答案 0 :(得分:0)
在JSX上下文中, FIND: (phone=\S*?digits=1).*
REPLACE: $1
表示JavaScript表达式。在JavaScript上下文中,{}
表示块或对象文字。
{}
位于JavaScript上下文(return {cell.data.row}
回调)中,由于.map
的位置,它们被解释为对象文字。
但是{...}
不是有效的对象文字,我怀疑你想在这里创建一个对象。使用
{cell.data.row}
代替。