【问题标题】:Using Axios to post data from React to PHP使用 Axios 将数据从 React 发布到 PHP
【发布时间】:2023-02-04 23:41:19
【问题描述】:

我已经将这部分编程为用于节点并且它工作正常,但根据请求(教授),她希望我使用基于 php 的后端来完成它。当我提交它时,它给我以下错误:

Access to XMLHttpRequest at 'http://localhost/lvs_stack/lvs_backend/AuthEngine.php' from origin 'http://localhost:5173' has been blocked by CORS policy: Request header field content-type is not allowed by Access-Control-Allow-Headers in preflight response.

更新路径错误,但现在我得到这个错误

我可能忽略了一些东西,所以我希望有人能帮助我。

header("Access-Control-Allow-Origin: *");
header("Access-Control-Allow-Headers: access");
header("Access-Control-Allow-Methods: GET, POST, OPTIONS, PUT, PATCH, DELETE");
header("Content-Type: application/json; charset=UTF-8");
header("Access-Control-Allow-Headers: Content-Type, Access-Control-Allow-Headers,Authorization, X-Requested-With");
header("Access-Control-Allow-Headers: X-Requested-With");

JSX 文件包含: Achternaam、Voornaam、Email 和 Cred 都是 useState

const handleLogIn = (e) => {
    e.preventDefault();
    Axios.post("http://localhost/LVS_Backend/DB_Conn.php", {
      achternaam: Achternaam,
      voornaam: Voornaam,
      email: Email,
    }).then((result) => {
      if (result.data.Status === "200") {
        localStorage.setItem("token", Cred);
        navigate("/");
      }
    });
  };
<form onSubmit={handleLogIn}>
<div className="border-b-4 w-48 border-blue-400">
          <input
            type="submit"
            name="submit"
            className="btn btn-success"
            value={`Continue As ${Voornaam}`}
          />
        </div>
</form>

【问题讨论】:

    标签: php reactjs axios cors vite


    【解决方案1】:

    标题是错误的

    header('Access-Control-Allow-Origin: *');
    header("Access-Control-Allow-Methods: GET, POST, OPTIONS, PUT, DELETE");
    header("Access-Control-Allow-Headers: Content-Type, Access-Control-Allow-Headers, X-Requested-With");
    

    现在工作正常

    【讨论】:

      猜你喜欢
      • 2019-04-12
      • 1970-01-01
      • 2020-11-26
      • 2020-02-05
      • 2020-07-01
      • 2022-11-11
      • 1970-01-01
      • 2019-03-03
      • 1970-01-01
      相关资源
      最近更新 更多