Axios请求上出现内部服务器错误500

时间:2020-04-16 08:48:53

标签: javascript reactjs server axios internals

我有http://localhost:81/log_api.php上运行的api,然后我在http://localhost:3000上运行的同一台计算机上有React应用程序。以下是React应用的代码:

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显示一些错误: enter image description here

我也直接访问了http://localhost:81/log_api.php,完全没有问题。它在浏览器上显示JSON数据。该问题仅在我在React中使用axios时发生。

文件名: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);

?>

网络屏幕截图: enter image description here

1 个答案:

答案 0 :(得分:0)

您可以尝试使用IP地址 _constructor_sliced

希望它会为您服务