I have api running at http://localhost:81/log_api.php, then I have React app on the same computer running at http://localhost:3000. Below is the codes for React app :
import React, { Component } from 'react';
import axios from 'axios';
class Logging extends Component {
state={
pl_log:[]
}
async componentDidMount(){
await axios.get("http://localhost:81/log_api.php")
.then(response=>this.setState(
{
pl_log: response.data
} ))
console.log(this.state)
}
render(){
return(
<div>
Logging
</div>
)
}
}
export default Logging;
Console.log shows some errors :
I have also directly visited http://localhost:81/log_api.php , no problem at all. It shows the JSON data on the browser. The problem only occurs when I use axios in React.
filename : log_api.php
<?php
$link = mysqli_connect("127.0.0.1:3307", "root", "root", "masterdb");
// Check connection
if($link === false){
die("ERROR: Could not connect. " . mysqli_connect_error());
}
$sql = $link -> query("SELECT * FROM pl_log");
$result=array();
while ($data = $sql -> fetch_assoc()){
$result[]=$data;
}
echo json_encode($result);
mysql_close($link);
?>