PHP文件没有在AJAX中运行?
我的AJAX函数没有运行我的PHP脚本。我有一个类似的功能,我的注册,它是没有任何问题的工作。AJAX函数运行到done调用的末尾,但是PHP文件中创建cookie的第一行没有运行 我已经将数据(结果)输出到控制台,看起来一切正常。没有出现错误,我已经在我完全控制的服务器上启用了PHP错误。服务器很好,因为网站的注册部分使用了这种精确的方法,并且它工作正常 JQueryPHP文件没有在AJAX中运行?,php,jquery,ajax,Php,Jquery,Ajax,我的AJAX函数没有运行我的PHP脚本。我有一个类似的功能,我的注册,它是没有任何问题的工作。AJAX函数运行到done调用的末尾,但是PHP文件中创建cookie的第一行没有运行 我已经将数据(结果)输出到控制台,看起来一切正常。没有出现错误,我已经在我完全控制的服务器上启用了PHP错误。服务器很好,因为网站的注册部分使用了这种精确的方法,并且它工作正常 JQuery $('#login-form').on("submit", function(e){ var da
$('#login-form').on("submit", function(e){
var dataString = $(this).serialize();
console.log(dataString);
$.ajax({
type: "POST",
url: "bin/login.php",
data: dataString
}).done(function (result, status, xhr) {
// Display message back to the user here.
window.location.replace('./app/');
console.log("Login Completed!");
}).fail(function (result) {
// TASK: Add a visual element on the page for errors.
if (result.status == 522){
alert("Email not verified!");
} else if (result.status == 523){
alert("Password was incorrect!");
} else if (result.status == 524) {
alert("User account not found!");
}
});
return false;
});
PHP
<?php
setcookie("TestCookie", "login.php", none, "/");
if (array_key_exists('emailAddress', $_POST) && array_key_exists('password', $_POST)){
$emailAddress = $_POST["emailAddress"];
$password = $_POST["password"];
// Connect to the database with this standard connect PHP script.
include('connectDB.php');
// Check the link was established.
if (!mysqli_connect_error()) {
$sqlget = "SELECT id, emailAddress, userPassword, userRole, emailVerified FROM users WHERE emailAddress=\"$emailAddress\"";
$sqldata = mysqli_query($dbconn, $sqlget);
if (mysqli_num_rows($sqldata) == 1){
// One user was found.
$userInfo = mysqli_fetch_array($sqldata);
setcookie("Account", "found", none, "/");
if ((password_verify($password, $userInfo["userPassword"])) {
setcookie("Password", "OK", none, "/");
if ($userInfo["emailVerified"] == true) {
setcookie("loginId", $userInfo["id"], none, "/");
} else {
// Email was not verified.
header('HTTP/1.1 522 Email Not Verified');
header('Conent-Type: application/json; charset=UTF-8');
die(json_encode($result));
}
} else {
// Password verification failed.
header('HTTP/1.1 523 Password verification Failed');
header('Conent-Type: application/json; charset=UTF-8');
die(json_encode($result));
}
} else {
// No user found in the system
header('HTTP/1.1 524 User not Found');
header('Conent-Type: application/json; charset=UTF-8');
die(json_encode($result));
}
} else {
// Place code to tell the user there was an internal DB error.
// Possibly a standard error message. Lets not scare the user.
header('HTTP/1.1 500 Internal Server Error');
header('Conent-Type: application/json; charset=UTF-8');
die(json_encode($result));
}
}
?>
我发现了问题。我在第18行的括号太多了。我将行更改为以下内容,并运行PHP脚本
if (password_verify($password, $userInfo["userPassword"])) {
if ($userInfo["emailVerified"] == true) {
setcookie("loginId", $userInfo["id"], 0, "/");
} else {
// Email was not verified.
header('HTTP/1.1 522 Email Not Verified');
header('Conent-Type: application/json; charset=UTF-8');
die(json_encode($result));
}
} else {
// Password verification failed.
header('HTTP/1.1 523 Password verification Failed');
header('Conent-Type: application/json; charset=UTF-8');
die(json_encode($result));
}
您的setcookie(“TestCookie”、“login.php”、none“/”)中的none
是什么
不依赖javascript来读取cookie,我只需要依靠PHP来验证他们是否登录了etc,并将错误消息返回给javascript。如果不希望cookie过期,请将none
更改为零(0)。@imvain2是一个更好的方法。谢谢你引起我的注意。尽管如此,仍然无法解释为什么PHP文件没有首先运行。@Kisaragi,谢谢。我确实看到了一条消息,没有人贬值。一旦我解决了这个问题,我就有一张便条要查。谢谢你为我节省时间。我更改了,但代码仍然无法运行。