首页  >  问答  >  正文

React.js / PHP / API

我正在尝试为网站创建一个管理面板(CRUD 操作)。我首先创建 API 端点,然后将其托管在我的子域上。我现在正在从该端点获取数据。显示所有联系人 (GET)、单个联系人 (GET) 和添加新联系人 (POST) 效果很好,但我在更新联系人 (PUT) 方面卡住了好几天。

预填表格工作正常。当我提交数据时,出现以下错误:

Access to XMLHttpRequest at 'https://api.itisgoodtohave.me/contacts/update.php' from origin 'http://localhost:3000' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: It does not have HTTP ok status.

PUT https://api.itisgoodtohave.me/contacts/update.php net::ERR_FAILED

Uncaught (in promise) AxiosError {message: 'Network Error', name: 'AxiosError', code: 'ERR_NETWORK', config: {…}, request: XMLHttpRequest, …}

update.php:

<?php

    header("Access-Control-Allow-Origin: *");
    header("Access-Control-Allow-Headers: *");
    header("Access-Control-Allow-Methods: *");

    include 'DbConnection.php';

    $objDb = new DbConnection;
    $conn = $objDb->connect();

    $method = $_SERVER['REQUEST_METHOD'];

    if ($method = "POST") {
        $contact = json_decode(file_get_contents('php://input'));
        $sql = "UPDATE contacts SET name=:name, email=:email, phone=:phone WHERE id=:id";
        $stmt = $conn->prepare($sql);
        $stmt->bindParam(':id', $contact->id);
        $stmt->bindParam(':name', $contact->name);
        $stmt->bindParam(':email', $contact->email);
        $stmt->bindParam(':phone', $contact->phone);
        if ($stmt->execute()) {
            $response = ['status' => 1, 'message' => "Contact updated sucessfully."];
        } else {
            $response = ['status' => 0, 'message' => "Could not update contact."];
        }
        echo json_encode($response);
    } else {
        echo "some error occured";
    }

EditContact.js

import React from 'react';
import axios from 'axios'; 
import { useState, useEffect } from 'react';
import { useNavigate, useParams } from 'react-router-dom';

export default function EditContact() {
  const [inputs, setInputs] = useState([])
  const navigate = useNavigate();
  const { id } = useParams();
  

 useEffect(() => {
    showContact();
 }, [])

function showContact() {
    axios.get(`https://api.mywebsite.com/contacts/single_read.php/?id=${id}`)  
    .then(function(response) {
        setInputs(response.data);
    })
}

    const handleChange = (event) => {
        const name = event.target.name;
        const value = event.target.value;
        setInputs(values=> ({...values, [name]: value}));
        console.log(inputs)
    }

    const handleSubmit = (event) => {
        event.preventDefault();
        axios.put('https://api.mywebsite.com/contacts/update.php', inputs)
        .then(function(response){
            console.log(response)
            navigate('/admin/contacts')
        })
    }


  return (
    <div>
      <h3>Edit Contact</h3>
      <form onSubmit={handleSubmit}>
            
            <label>Name</label>
            <input defaultValue={inputs.name} type="text" name="name" onChange={handleChange} />
            <br />

            <label>Email</label>
            <input defaultValue={inputs.email} type="text" name="email"  onChange={handleChange} />
            <br />

            <label>Phone</label>
            <input defaultValue={inputs.phone} type="text" name="phone" onChange={handleChange} />
            <br />

            <button type="submit">Save</button>

        </form>     

    </div>
  )
}

我已经尝试了邮递员中的每个端点。所有这些(GET、POST、PUT、DELETE)都在工作。这些条目已在我的数据库中更新。

经过几个小时的 CORS 谷歌搜索、再次重写我的代码等之后,我就是无法理解。我对编码还很陌生,但对我来说,POST 有效但 PUT 无效似乎不合逻辑(或者我在这里缺少一些基本逻辑)。

如果有人能提出新想法,我会很高兴。我渴望学习:)

P粉321584263P粉321584263251 天前256

全部回复(1)我来回复

  • P粉726133917

    P粉7261339172024-01-17 00:52:29

    Postman 是一个 HTTP 客户端,而不是浏览器。 CORS 适用于浏览器,因此 HTTP 客户端将忽略这些标头。因此,使用 Postman 调用 API 时不会出现任何问题,这是完全有道理的。

    错误消息的重要部分是对预检请求的响应未通过访问控制检查:它没有 HTTP 正常状态预检请求是带有 OPTIONS 方法的 HTTP 请求。您可以将此视为侦察请求,以检查是否允许发出进一步的请求 (请参阅 MDN 文档)。此错误表明此 OPTIONS 请求的响应不是 HTTP 200。此处的解决方案是确保 OPTIONS 请求将导致设置了所有 CORS 标头的 HTTP 200。

    回复
    0
  • 取消回复