注册提交报错Unexpected end of JSON input 数据未入库求助
注册功能报错、数据无法提交问题修复
报错核心原因
控制台抛出的Uncaught SyntaxError: Unexpected end of JSON input本质是前端拿到的后端返回内容不是合法JSON格式,根源是前后端代码存在多处语法和逻辑错误,具体问题点如下:
- 后端PHP无执行入口:
Controller类定义完成后,文件末尾没有主动执行new Controller();触发业务逻辑,请求进来后PHP只加载类定义、不跑业务代码,返回空响应,前端对空字符串执行JSON.parse直接触发语法错误。 - 后端存在递归实例化bug:原代码把
new Controller();写在了addUser()方法的最后,一旦触发业务逻辑就会无限递归实例化类,导致响应内容重复、PHP执行超时。 - 后端请求方法判断写错:构造函数中
$_SERVER['REQUEST_METHOD'] = "POST"用了单等号赋值运算符,不是比较运算符,判断逻辑永远成立。 - 后端字段验证逻辑写反:年龄、邮箱的验证判断条件倒置,
filter_var验证通过时反而抛出格式错误提示,合法输入会被拦截。 - 后端密码存储逻辑无效:代码中生成了密码哈希
$hash,但插入数据库时仍然使用明文$password,哈希操作完全没生效,同时直接拼接SQL语句存在SQL注入风险。 - 前端性别选择器写错:获取性别值用了
$(".input[name='gender']:checked"),多写了class选择器的.,input是标签名应该用$("input[name='gender']:checked"),导致性别值永远获取不到。 - 前端确认密码判断写错:两次复用了
error_password的判断条件,确认密码的错误提示逻辑永远不会正常触发。 - 前端页面刷新逻辑失效:
location.reload是函数,必须加括号写成location.reload()才会执行页面刷新。 - 前后端响应格式不统一:后端校验失败返回JSON、注册成功返回数字1,前端没有做响应格式兼容,很容易因为返回内容带多余空白、冗余内容导致JSON解析失败。
修复后代码
前端 script.js
$(document).ready(function(){ $(".save").click(function(){ let user = { name: $(".name").val(), surname: $(".surname").val(), age: $(".age").val(), // 修复性别选择器,去掉多余的类选择器标识 gender: $("input[name='gender']:checked").val(), email: $(".email").val(), password: $(".password").val(), confirm: $(".confirm").val() } $.ajax({ type: "post", url: "server.php", data: {user: user, action: "ajax1"}, dataType: "json", // 明确指定响应格式为JSON,让jQuery自动完成解析 success: function(r){ console.log(r); if(r.code == 1){ console.log("注册成功"); location.reload(); // 加括号执行刷新函数 }else{ if("error_name" in r){ $(".name").val(""); $(".name").attr("placeholder", r.error_name); } if("error_surname" in r){ $(".surname").val(""); $(".surname").attr("placeholder", r.error_surname); } if("error_age" in r){ $(".age").val(""); $(".age").attr("placeholder", r.error_age); } if("error_email" in r){ $(".email").val(""); $(".email").attr("placeholder", r.error_email); } if("error_gender" in r){ $(".gender-tip").text(r.error_gender); } if("error_password" in r){ $(".password").val(""); $(".password").attr("placeholder", r.error_password); } // 修复确认密码的判断条件 if("error_confirm" in r){ $(".confirm").val(""); $(".confirm").attr("placeholder", r.error_confirm); } } } }) }) })
后端 server.php
<?php class Controller{ function __construct(){ // 修复请求方法判断,单等号改为比较运算符 if($_SERVER['REQUEST_METHOD'] == "POST"){ $this->db = new mysqli('localhost', 'root', 'root', 'Levon'); if(isset($_POST['action'])){ if($_POST['action'] == "ajax1"){ $this->addUser(); } } } } function addUser(){ extract($_POST['user']); $error = []; if(empty($name)){ $error['error_name'] = "Fill the name"; } if(empty($surname)){ $error['error_surname'] = "Fill the surname"; } if(empty($age)){ $error['error_age'] = "Fill the age"; // 修复验证逻辑:验证失败才抛错,增加取反判断 }else if(!filter_var($age, FILTER_VALIDATE_INT)){ $error['error_age'] = "Write age correctly"; } if(empty($email)){ $error['error_email'] = "Fill the Email"; // 修复邮箱验证逻辑 }else if(!filter_var($email, FILTER_VALIDATE_EMAIL)){ $error['error_email'] = "Write Email correctly"; } if(empty($gender)){ $error['error_gender'] = "Select the gender"; } if(empty($password)){ $error['error_password'] = "Select the password"; }else if(strlen($password) < 6){ $error['error_password'] = "Password is less than 6"; } if(empty($confirm)){ $error['error_confirm'] = "Fill the confirm password"; } if($password !== $confirm){ $error['error_confirm'] = "Password didn't match"; } if(count($error) > 0){ // 统一返回JSON格式,设置响应头 header('Content-Type: application/json'); echo json_encode($error); }else{ $hash = password_hash($password, PASSWORD_DEFAULT); // 修复SQL注入问题,用预处理语句,同时存储哈希后的密码 $stmt = $this->db->prepare("INSERT INTO `users` (`name`, `surname`, `age`, `gender`, `email`, `password`) VALUES (?, ?, ?, ?, ?, ?)"); $stmt->bind_param("ssisss", $name, $surname, $age, $gender, $email, $hash); $stmt->execute(); header('Content-Type: application/json'); echo json_encode(['code' => 1]); } } } // 类定义完成后在文件末尾实例化,移除addUser方法里的递归实例化代码 new Controller(); ?>
额外说明
修复后所有响应统一为JSON格式,前端不需要手动执行JSON.parse,jQuery会自动处理,从根源上避免JSON解析报错。同时修复了SQL注入和明文存密码的安全隐患,所有表单字段的校验逻辑都能正常触发。
内容的提问来源于stack exchange,提问作者Ernast
相关产品推荐
相关产品推荐

