簡體   English   中英

請求在 createError 處失敗,狀態碼為 404

[英]Request failed with status code 404 at createError

我正在嘗試從表中獲取數據,例如 API 和 AXIOS 到 React 前端。 我正在使用代理來獲取數據,並且當文件位於遠程服務器上時它工作正常( http://auto-parts.oaccoman.com/test.php )。 當我使用來自本地(http://localhost:81/data/index.php)的相同數據時,它會在控制台中顯示錯誤。

Error: Request failed with status code 404

數據和表單.jsx

import React from 'react'
 

class DataandForm extends React.Component{
    constructor()
    {
        super()
        this.state={
            row:[],
            logged:true
        }
    }

    componentDidMount()
    {
        const axios = require('axios');
        
         
         const remote="http://auto-parts.oaccoman.com/test.php"
          

         const proxy="https://cors-anywhere.herokuapp.com/"
         const local="http://localhost:81/data/index.php"
        
        axios
          .get(proxy+local)
          .then( response =>{
            // handle success
            console.log(response);
         this.setState({row:response.data});
          
          })
          .catch(function (error) {
            // handle error
          console.log(error);
          })
          .then(function () {
            // always executed
          
          });

          setTimeout(prests=>{
           this.setState({
            logged:false.logged
           })
          }, 2500);

          
        
    }
    
    render()
    {
        return(

<div className="container">
    <div className="row">

        {this.state.logged?'Loading Your Data...' : <div className="col-sm-6">
           {this.state.row.map(datum=> 
            <div className="row">
                <div className="col-sm-4">key={datum.id}</div>
                <div className="col-sm-4">data1={datum.name}</div>
                {/* <div className="col-sm-4">data2={datum.value}</div> */}
               

            </div>


            
            )}
        </div>}
        

    <div className="col-sm-10 offset-sm-2"><br/>

<form class="form-inline" action=" " method="post" >
<div class="form-group">
  <label for="email">FIRSTNAME:</label>
  <input type="text" name="data1" class="form-control" id="email" />
</div>
<div class="form-group">
  <label for="pwd">LASTNAME:</label>
  <input type="text" name="data2" class="form-control" id="pwd" />
</div>
 
<button type="submit" class="btn btn-danger">Submit</button>

</form>

</div>
    </div>
</div>

        )
    }
}


export default DataandForm

http://localhost:81/data/index.php

<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "mydata";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);

// Check connection
if ($conn->connect_error) {
  die("Connection failed: " . $conn->connect_error);
}



$sql = "SELECT * FROM datum";
$result = $conn->query($sql);

$jsonarray= [];

if ($result->num_rows > 0) {
   
  while($row = $result->fetch_assoc()) {
     
    $jsonarray[]=$row;
  }
} else {
  echo "0 results";
}

 

 
 echo json_encode($jsonarray);
  

?>

如果不需要從 localhost 訪問代理(並且您知道為什么需要這樣做),我想您可以確定您是在本地運行還是在遠程服務器中運行(通過某種配置)然后運行一些東西與此類似:

let url
const proxy="https://cors-anywhere.herokuapp.com/"
const local="http://localhost:81/data/index.php"

if (runningLocally) // usually some way to determine a "development mode"
    url = local
else
    url = proxy + local

(這可以寫得更短,我這樣做只是為了清楚)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM