jQuery AJAX 登录问题(JSON 解析失败)
Posted
技术标签:
【中文标题】jQuery AJAX 登录问题(JSON 解析失败)【英文标题】:Problem with jQuery AJAX login (JSON parse fail) 【发布时间】:2021-11-04 23:44:36 【问题描述】:我的 jQuery AJAX 登录有问题。当我尝试登录控制台时,会弹出一条消息“Requested JSON parse failed.”。代码的主要目的是登录到受简单 jQuery 幻灯片验证码保护的管理仪表板,因为我不喜欢 reCAPTCHA。那是我的 jQuery AJAX 调用:
$(".slidercaptcha").hide();
$( "#login" ).on( "submit", function( event )
event.preventDefault();
$(".beforeContinue").hide();
$(".slidercaptcha").fadeIn('fast');
);
var Toast = Swal.mixin(
toast: true,
position: 'top-end',
showConfirmButton: false,
timer: 4000
);
var captcha = sliderCaptcha(
id: 'captcha',
repeatIcon: 'fa fa-redo',
onSuccess: function()
$.ajax(
url: 'ajax_requests/login_ajax.php',
type: 'POST',
cache: false,
data:
'username': $('#username').val(),
'password': $('#password').val()
,
contentType: "application/json",
dataType: 'JSON',
success: function(response)
Toast.fire(
icon: 'success',
title: "Successfully logged in! Redirecting..."
);
setTimeout(function()
if (response.status == "1")
location.href = "index.php";
else if (response.status == "2")
location.href = "index.php";
else
Toast.fire(
icon: 'error',
title: "Something went wrong, please try again later!"
);
, 2000);
,
error: function(jqXHR, exception)
var msg = '';
if (jqXHR.status === 0)
msg = 'Not connect.\n Verify Network.';
else if (jqXHR.status == 404)
msg = 'Requested page not found. [404]';
else if (jqXHR.status == 500)
msg = 'Internal Server Error [500].';
else if (exception === 'parsererror')
msg = 'Requested JSON parse failed.';
else if (exception === 'timeout')
msg = 'Time out error.';
else if (exception === 'abort')
msg = 'Ajax request aborted.';
else
msg = 'Uncaught Error.\n' + jqXHR.responseText;
console.log(msg);
);
);
这就是我的 PHP 表单(login_ajax.php):
<?php header('Content-Type: application/json');
$conn = mysqli_connect("localhost","root","","shopperapproved");
$username = $conn->real_escape_string($_POST['username']);
$password = $conn->real_escape_string($_POST['password']);
$password_crypted = hash('sha256', $conn->real_escape_string($password));
$_login = $conn->prepare("SELECT * FROM `admin_users` WHERE `username`=? AND `password`=?");
$_login->bind_param("ss", $username, $password_crypted);
$_login->execute();
if ($_login->num_rows > 0)
$_SESSION['login_session'] = hash('sha256', rand(6453, 891673));
if (isset($_SESSION['login_session']))
$result = array("status" => "1");
echo json_encode($result);
else
$result = array("status" => "0");
echo json_encode($result);
else
$result = array("status" => "0");
echo json_encode($result);
$_login->close();
?>
提前致谢。
【问题讨论】:
【参考方案1】:如果你做了,你会得到什么:
$res = json_encode($result);
print_r($res)
echo json_encode($result);
还要检查您的 ajax 调用:
success: function(response)
var data = JSON.parse(response);
console.log(data)
... your code
我认为你的 json 字符串不对。
我在你的代码中看到了:
if ($_login->execute())
所以什么都不会被执行 示例:
$_login->execute();
$result = $_login->get_result();
$user = $result->fetch_array(MYSQLI_ASSOC);
if ($user !== false)
$json = json_encode($user);
print_r($json); // You should get back and Json String.
else
// there where no results
【讨论】:
嘿伙计,我有什么问题。问题是表单中的 POST 数据没有通过并且 PHP 脚本没有收集它。我意识到当我手动输入 $_POST['username'] = "admin";和 $_POST['password'] = "admin"; 在 PHP 脚本中,它返回一个包含状态的数组。那么如何让这个 POST 数据(用户名和密码)通过这个过程呢?顺便说一句,我从您的 php 代码中返回 SyntaxError: JSON.parse: unexpected character at line 1 column 1 of the JSON data 更新了我的代码。试试上面的 JSON.parse。 我回显一个数组,其状态值取决于数据库记录(用户名和密码是否匹配)。状态值可以是 0 和 1。 0 是错误的凭据,1 是信息匹配。 当代码甚至没有到达该部分时,尝试在 success: function(response)... 部分中添加此代码毫无意义。 你使 if ($_login->execute()) 所以有一个 if.....什么都不会被执行。以上是关于jQuery AJAX 登录问题(JSON 解析失败)的主要内容,如果未能解决你的问题,请参考以下文章