我试图只从带有ReactJS和Axios的PHP文件中获取"Test"响应。但是从休息中获取整个代码.php而不是"测试"。
当我尝试访问本地主机/休息时.php只得到"测试"字,PHP 运行良好。
我做错了什么?
谢谢
app.jsx
import React, { Component } from 'react';
import './App.css';
import axios from 'axios';
class App extends Component {
constructor(props) {
super(props);
this.state = {
};
}
componentDidMount() {
const url = './api/rest.php'
axios.get(url)
.then(res => {
const persons = res.data;
this.setState({ contacts: persons });
})
}
render() {
return (
<div style={{ width: "100%", height: "100vh" }}>
{this.state.contacts}
</div>
)
}
}
export default App;
和休息.php
header("Access-Control-Allow-Origin: *");
$data = json_decode(file_get_contents("php://input"), true);
$host = "localhost";
$user = "root";
$password = "";
$dbname = "musteri";
//$con = mysqli_connect($host, $user, $password,$dbname);
echo "test";
实际上axios.get(url)
读取PHP本地文件,而不是PHP脚本的结果。
如果你想要解释版本,你必须设置url = 'http://the-server/api/rest.php'
(用你的Apache服务器替换"the-server",也许是"localhost"(