我正在尝试创建一个登录系统。但是,我收到JSON输入的意外结束,并且不确定为什么会因为它在我正在使用的另一个页面上起作用。就是说JS第12行的JSON输入意外结束。
我正在使用的JS是:
$('#formUserLogin').submit(function(event){ // If data is submitted from the form called formUserLogin
formData = $('#formUserLogin').serialize(); // Serialize data
event.preventDefault();
$.ajax({
type: "POST", // Defines type for ajax function
url: "policeLoginDAO.php", // URL to send data to
data: formData+"&phpFunction=login", // Data to send + PHP function to use it with
datatype: 'json', // Datatype to send
success: function(msg){
dataJson = JSON.parse(msg);
if(dataJson['result']=='false') { // If result is false do this:
$("divMessage").html("Wrong username or password"); // Display message in divMessage div in html page
} else { // Else if it's correct, do this:
window.location="../policeHome.php"; // Open page, policeHome.php and set the value of badgeno to the stored badgeno in session storage
}
}
});
});
我正在使用的PHP是:
<?php
if(isset($_POST['phpFunction'])) {
if($_POST['phpFunction'] == 'login') {
login();
}
}
function login() {
session_start(); // Starts the session for the user
include "../../config.php"; // Includes the config file to connect the the database
$uName = strip_tags($_POST['userName']); // Strips the tags for the username
$pWord = strip_tags($_POST['password']); // Strips the tags for the password
$userID = $_SESSION['uName']; // Sets the username to the session
$uName1 = strip_tags(mysqli_real_escape_string($connection, $uName)); // Strips the tags and escapes the string for the username
$pWord1 = strip_tags(mysqli_real_escape_string($connection, $pWord)); // Strips the tags and escapes the string for the password
$sql = "SELECT * FROM `tbl_police` WHERE Username='".$uName1."' "; // Selects all data from the tbl_user where email is equal to the user input
$result = $connection->query($sql); // Result is equal to the queries outcome
$pass = $row['Password']; // Gets the password
if ($result->num_rows === 1) { // If the result is identical to 1
$row = mysqli_fetch_array($result); // Fetch result
if (password_verify($pWord1, $row['Password'])) { // Verify the password against the users input by using the password_verify function. If it's correct... Do this:
echo json_encode($row);
$_SESSION['uName'] = $uName;
}else{ // If the password is not correct... Do this:
echo("Hello");
}
mysqli_close($connection); // Close the connection to the database
}
}
?>
任何帮助将不胜感激,谢谢
伙计,我认为您需要稍微修改代码。您需要在成功函数中指定目标元素的属性,即jQuery函数调用的元素是类还是ID。