尝试上帝之爱从我的数据库中获取数据并进入我的React Component状态,但是无法理解我做错了什么。这是我的PHP文件:
<?php
$servername = "localhost";
$username = "root";
$password = "root";
$dbname = "maggan";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT * FROM Items";
$result = $conn->query($sql);
while($row = mysqli_fetch_assoc($result))
$items[] = $row;
echo json_encode($items);
$conn->close();
?>
这是我的React组件:
import React, { Component } from 'react';
// Component import
import Menu from './components/menu';
import Footer from './components/footer';
import ProductContainer from './components/productContainer';
import CategoryContainer from './components/categoryContainer';
class Archive extends React.Component {
constructor(props){
super(props);
this.state = {
products: [],
category: ""
}
this.filterHandler = this.filterHandler.bind(this);
}
// Set component state to the currently clicked "cat" (CategoryItem)
filterHandler(tag){
this.setState({
category: tag
})
}
componentDidMount(){
fetch("/getProducts", {
method: "GET",
headers: {
"Content-Type": "application/json"
}
})
.then(async res => {
const data = await res.json();
setState({
products: data
})
})
.catch(function(err) {
console.log('ERROR!!! ' + err.message);
});
}
render() {
// 1. Render CategoryContainer with props products and filterHandler function to show all uniqe CategoryItems and filter products based on category
// 2. Render ProductContainer based on category. If this.state.category.length is true - filter "prod" & where prod.categories is same type and name as this.state.category : else render all this.state.categories that matches "paint".
return (
<div>
<Menu />
<div className="archive-container">
<div className="archive-wrapper">
<CategoryContainer
filterHandler={this.filterHandler}
products={this.state.products}
/>
<br/><br/>
<ProductContainer
products={this.state.category.length
? this.state.products.filter((prod) => prod.category === this.state.category)
: this.state.products.filter((prod) => prod.category === 'paint')
}
/>
</div>
</div>
<Footer />
</div>
);
};
};
export default Archive;
我使用webpack捆绑我的项目,我的条目是:
const entry = [
'webpack-dev-server/client?http://localhost:8080', // bundle the client for webpack-dev-server and connect to the provided endpoint
'webpack/hot/only-dev-server', // bundle the client for hot reloading only- means to only hot reload for successful updates
'./app.js'
]
..我的dev-server.js文件如下所示:
var WebpackDevServer = require('webpack-dev-server');
var webpack = require('webpack');
// requiring my webpack configuration
var config = require('./webpack.config.js');
var path = require('path');
var compiler = webpack(config);
// then spinning up a new dev server with some settings
var server = new WebpackDevServer(compiler, {
hot: true,
filename: config.output.filename,
publicPath: config.output.publicPath,
proxy: {
"/getMail": 'http://localhost:80/magdan/php/mailer.php',
"/getProducts": 'http://localhost:80/magdan/php/products.php'
},
stats: {
colors: true
}
});
// its gonna listen to port 8080
server.listen(8080, 'localhost', function() {
console.log("Starting server on http://localhost:8080");
});
我只会收到错误:
ERROR !!!位于0的JSON中的意外标记C
我非常感谢任何类型的输入!
答案 0 :(得分:2)
您的PHP文件未输出JSON。
您的提取处理程序正在调用res.json()
:
const data = await res.json();
预计输出格式正确JSON,但您的PHP文件输出“已连接”:
echo "Connected successfully";
您需要更改PHP文件以返回有效的JSON。