在前端我使用ReactJs,我一直在嘗試通過(guò)axios.get從后端(PHP)獲取登錄用戶(hù)的數(shù)據(jù),為用戶(hù)設(shè)置一個(gè)條件,如果滿(mǎn)足,他們將返回登錄頁(yè)面后端沒(méi)有返回任何內(nèi)容,出現(xiàn)返回登錄的情況是因?yàn)榍岸素?fù)責(zé)處理數(shù)據(jù)的文件是空的,所以沒(méi)有返回任何內(nèi)容。
我已經(jīng)設(shè)置了會(huì)話(huà),將 $_SESSION[] 替換為 $_POST[],并將這些代碼片段放置在頁(yè)面頂部:
error_reporting(E_ALL); ini_set('display_errors','1');
但沒(méi)有任何效果,并且錯(cuò)誤沒(méi)有顯示。我也在 Postman 中進(jìn)行了測(cè)試,它返回空,但是當(dāng)我在 x-www-form-urlencoded 中測(cè)試時(shí),所有內(nèi)容都已設(shè)置并以任何可能的方式寫(xiě)入鍵和值,它返回 400 錯(cuò)誤,所有字段都必須填寫(xiě).
在下面,我將發(fā)布登錄和注冊(cè)的代碼片段(其中處理用戶(hù)的輸入以發(fā)送到數(shù)據(jù)庫(kù))。
登錄
$db = _db(); try { $q=$db->prepare("SELECT * FROM users WHERE email = :email"); $q->bindValue(":email", $_POST["email"]); $q->execute(); $row = $q->fetch(); if(!empty($row)){ if (password_verify($_POST["password"], $row["password"])) { $user = array( "user_id" => $row["user_id"], "first_name" => $row["first_name"], "last_name" => $row["last_name"], "email" => $row["email"], "company" => $row["company"], "phone_number" => $row["phone_number"], "verified" => $row["verified"], "token" => $row["token"], ); header("Content-type: application/x-www-form-urlencoded"); http_response_code(200); echo json_encode($user); exit(); } else { header("Content-type: application/x-www-form-urlencoded"); http_response_code(400); echo json_encode("Wrong email or password"); exit(); } } else { header("Content-type: application/x-www-form-urlencoded"); http_response_code(400); echo json_encode("User does not exist"); exit(); }
注冊(cè) - 也在這里設(shè)置 $_SESSION,因?yàn)樵诖_認(rèn)帳戶(hù)后,我計(jì)劃將用戶(hù)直接重定向到儀表板
$q=$db->prepare("INSERT INTO users(user_id, first_name, last_name, company, email, phone_number, password, forgot_password, token, verified) VALUES(:user_id, :first_name, :last_name, :company, :email, :phone_number, :password, :forgot_password, :token, :verified)"); $q->bindValue(":user_id", null); $q->bindValue(":first_name", $firstName); $q->bindValue(":last_name", $lastName); $q->bindValue(":company", $company); $q->bindValue(":email", $email); $q->bindValue(":phone_number", $phoneNumber); $q->bindValue(":password", $passwordHash); $q->bindValue(":forgot_password", $forgotPass); $q->bindValue(":token", $token); $q->bindValue(":verified", false); $q->execute(); $user_id = $db->lastInsertId(); $to_email = $email; $subject = "Email subject"; $message = "Click on the following link to verify your account: <a href='http://localhost/api/confirm_account.php?token=$token'>Confirm your account</a>"; require_once(__DIR__."/emailVerification/send_email.php"); $user = array( "user_id" => $_SESSION["user_id"] = $row["user_id"], "first_name" => $_SESSION["first_name"] = $row["first_name"], "last_name" => $_SESSION["last_name"] = $row["last_name"], "email" => $_SESSION["email"] = $row["email"], "company" => $_SESSION["company"] = $row["company"], "phone_number" => $_SESSION["phone_number"] = $row["phone_number"], "verified" => $_SESSION["verified"] = $row["verified"], "token" => $_SESSION["token"] = $row["token"], ); header("Content-type: application/json"); http_response_code(200); echo json_encode($user); exit(); } }
這就是我在前端處理 POST/GET 請(qǐng)求的方式
const handleLogInSubmit = (event) => { event.preventDefault(); axios .post("http://localhost/api/login.php", { email: userInput.email, password: userInput.password, }) .then((response) => { if (response.data) { navigate("/dashboard"); } else { setErrorMessage("Login failed, try again"); } }) useEffect(() => { axios.get("http://localhost/api/login.php") .then((response) => { if(response.data){ setUser(response.data); console.log(response.data) } else { console.log(response) navigate("/login"); } }); }, [navigate]);
login.php 文件的輸出
如果您需要查看其余的前端和后端代碼,請(qǐng)告訴我。
編輯:
現(xiàn)在,我可以看到代碼出了什么問(wèn)題,但即使在收到消息登錄成功之后,我仍然不斷被重定向到登錄頁(yè)面,只是看到 axios.get不檢索任何內(nèi)容,僅 axios.post 起作用:
問(wèn)題是您從前端發(fā)送 JSON
數(shù)據(jù),但嘗試使用 $_POST
來(lái)提取它 根據(jù)文檔適用于 x-www-form-urlencoded
或 multipart/form-data
表單數(shù)據(jù)。
您可以發(fā)送后端期望的數(shù)據(jù),而不是發(fā)送 JSON 數(shù)據(jù),這是一串查詢(xún)參數(shù):
.post("http://localhost/api/login.php", new URLSearchParams({ email: userInput.email, password: userInput.password, }))