gpt4 book ai didi

javascript - 将 PHP 的 http 响应代码返回到 AJAX

转载 作者:行者123 更新时间:2023-11-28 17:33:53 24 4
gpt4 key购买 nike

我正在尝试为网站制作登录页面。我有一个函数,它使用 AJAX 向 PHP 脚本发送请求,以检查是否输入了正确的用户名和密码。如果查询返回成功结果,我发送 http_response_code(200),否则我发送 http_response_code(403) 。但是,登录功能似乎没有返回任何响应状态。响应似乎未定义。在这种情况下,即使输入了正确的密码和用户名,该功能也会针对错误的密码或用户名发出窗口警报。我应该检查什么条件来确定 success 函数应该根据 http 响应代码执行什么操作?是否有另一种方法可以根据 PHP 脚本的功能将条件返回到 AJAX?

这是登录功能的代码。

function login(){
var username = document.getElementById("username").value;
var password = document.getElementById("password").value;
var dataString = 'username1=' + username + '&password1=' + password;
if (username == '' || login == ''){
window.alert("Please fill in username or password.");
}
else{
$.ajax({
type: "POST",
url: "login.php",
data: dataString,
cache: false,
crossDomain : true,
success: function(response) {
if (response.status == 200) {
window.location = 'http://localhost/site.html';
}
else {
window.alert("The password or username you have entered is not valid");
}
}
});
}
return false;

}

这是我的 php 脚本。

<?php
header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Methods: GET, POST, PATCH, PUT, DELETE, OPTIONS');
header('Access-Control-Allow-Headers: Origin, Content-Type, X-Auth-Token');
$password2 = $_POST['password1'];
$username2 = $_POST['username1'];
$connection = mysqli_connect("localhost", "root", "password", "database") or die("Unable to connect to MySQL");
$query = mysqli_query($connection, "SELECT * FROM users where username = '$username2' AND password = '$password2'") or die(mysqli_error($connection));
$row = mysqli_fetch_array($query, MYSQLI_BOTH) or die(mysqli_error($connection));
if(!empty($row['username']) AND !empty($row['password'])) {
session_start();
$_SESSION['username'] = $username2;
http_response_code(200);
echo "Successful Login";
exit;
}
else{
http_response_code(403);
echo "The password or username you have entered is not valid";
}
mysqli_close($connection);
?>

最佳答案

当您检查响应并发送 get response.status 时,您手中实际上并没有数组或对象作为响应:

因此,在检查登录时,您可以创建一个包含 statusmessage 以及 json_encode() 的数组,以便您的 JavaScript 代码可以拿起它并阅读它。

<?php
// fix your query connection - you are currently vulnerable. It does go outside of the scope of your question so I am not going to tackle it here.
if(!empty($row['username']) AND !empty($row['password'])) {
session_start();
$_SESSION['username'] = $username2;
$return = array(
'status' => 200,
'message' => "Login Successful."
);
http_response_code(200);
}
else{
$return = array(
'status' => 403,
'message' => "Login attempt denied."
);
http_response_code(403);
}
print_r(json_encode($return));

现在您可以在 AJAX 函数中获取响应:

    success: function(response) {
var data = $.parseJSON(response);
if (data.status == 200) {
window.location = 'http://localhost/site.html';
}
else {
window.alert(data.message);
}
}

关于javascript - 将 PHP 的 http 响应代码返回到 AJAX,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49604495/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com