php Reactaxios删除得到错误,我怎么能修复它?

zpqajqem  于 2023-01-24  发布在  PHP
关注(0)|答案(1)|浏览(123)

我用Php API从mysql取数据,用postman检查,都通过了,我用react作为前端,目前只有删除是不工作的,它会显示<br /> <b>Notice</b>: Trying to get property 'id' of non-object in <b>C:\xampp\htdocs\mypost\api\delete.php</b> on line <b>22</b><br /> {"message":"Post Deleted"}即使我把硬代码仍然是一样的,格式看起来不错,不能找出哪一个是错误的,有人能帮助吗?

//this is react page 

import { useEffect } from "react";
import axios from "axios";
import { useNavigate,useParams } from "react-router-dom";
export default function Delete(){
    const navigate=useNavigate();
    const {id}=useParams();   
    const harcode={"id":11};
    useEffect(()=>{
    deletePost();
    },[]);
//currently using hard code but still show same error
 const deletePost=()=>{ 
    axios.delete('http://localhost:8081/mypost/api/delete.php',{"id":11})
    .then(res=>{
     console.log(res.data)
     navigate('/read');
 }).catch(err=>console.log(err))}
    return(
        <h1>Delete my Post</h1>
    )
}

//this is php delete page 

<?php 
  // Headers
  header('Access-Control-Allow-Origin: *');
  header('Content-Type: application/json');
  header('Access-Control-Allow-Methods: DELETE');
  header('Access-Control-Allow-Headers: Access-Control-Allow-Headers,Content-Type,Access-Control-Allow-Methods, Authorization, X-Requested-With');

  include_once '../config/Database.php';
  include_once '../models/Post.php';

  // Instantiate DB & connect
  $database = new Database();
  $db = $database->connect();

  // Instantiate blog post object
  $post = new Post($db);

  // Get raw posted data
  $data = json_decode(file_get_contents("php://input"));

  // Set ID to update
  $post->id = $data->id;

  // Delete post
  if($post->delete()) {
    echo json_encode(
      array('message' => 'Post Deleted')
    );
  } else {
    echo json_encode(
      array('message' => 'Post Not Deleted')
    );
  }

//this is php post page

<?php 
  // Headers
  header('Access-Control-Allow-Origin: *');
  header('Content-Type: application/json');
  header('Access-Control-Allow-Methods: DELETE');
  header('Access-Control-Allow-Headers: Access-Control-Allow-Headers,Content-Type,Access-Control-Allow-Methods, Authorization, X-Requested-With');

  include_once '../config/Database.php';
  include_once '../models/Post.php';

  // Instantiate DB & connect
  $database = new Database();
  $db = $database->connect();

  // Instantiate blog post object
  $post = new Post($db);

  // Get raw posted data
  $data = json_decode(file_get_contents("php://input"));

  // Set ID to update
  $post->id = $data->id;

  // Delete post
  if($post->delete()) {
    echo json_encode(
      array('message' => 'Post Deleted')
    );
  } else {
    echo json_encode(
      array('message' => 'Post Not Deleted')
    );
  }
gzszwxb4

gzszwxb41#

使用此方法

const deletePost=(id)=>{ 
        axios.delete(`http://localhost:8081/mypost/api/delete.php/${id}`)
        .then(res=>{
         console.log(res.data)
         navigate('/read');
     }).catch(err=>console.log(err))}
        return(
            <h1>Delete my Post</h1>
        )
    }

相关问题