React+PHP交互问题:登录后无法通过Axios获取用户数据
问题:React通过Axios获取已登录用户数据失败,导致自动跳转登录页
前端采用React开发,尝试通过axios.get从PHP后端获取已登录用户数据,逻辑为:若后端无数据返回则跳转至登录页。但目前后端处理文件无返回值,导致用户被强制跳回登录页。
已尝试以下操作但未解决:
- 配置会话(Session)
- 将
$_SESSION[]替换为$_POST[] - 添加错误报告代码:
error_reporting(E_ALL); ini_set('display_errors','1');
上述错误报告代码未生效,且无任何错误信息显示。Postman测试结果:
- GET请求返回空内容
- 使用
x-www-form-urlencoded格式发送POST请求时,返回400错误,提示需填写所有字段
相关代码片段
登录接口(PHP)
$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(); }
注册接口(PHP)- 配置$_SESSION实现验证后跳转仪表盘
$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请求处理(React)
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]);
更新
已定位部分问题,但仍存在异常:显示“登录成功”后会被自动跳转回登录页。经排查,原因是axios.get请求无数据返回,仅axios.post请求能正常获取用户数据:
- POST请求登录:正常返回用户数据
- GET请求登录:返回空内容
内容的提问来源于stack exchange,提问作者meii18
相关产品推荐
相关产品推荐

