我使用以下代码进行登录屏幕,该屏幕使用ajax调用通过PHP脚本检查用户信用。正如你所看到的那样,我已经进入了一个有效的控制台调用,但是每次都会跳过if调用。
UPDATE ***我已经意识到,当我将响应发送到控制台时,它似乎是一个空行,然后是新行上的PHP脚本的回显。
Ajax Call
$(document).ready(function(){
// Variable to hold request
var request;
// Bind to the submit event of our form
$("#log").submit(function(event){
// Prevent default posting of form
event.preventDefault();
// Abort any pending request
if (request) {
request.abort();
}
// setup some local variables
var $form = $(this);
// Let's select and cache all the fields
var $inputs = $form.find("input, select, button, textarea");
// Serialize the data in the form
var serializedData = $form.serialize();
// Disable the inputs for the duration of the Ajax request.
$inputs.prop("disabled", true);
// Fire off the request
request = $.ajax({
url: "../php/auth.php",
type: "post",
data: serializedData
});
request.done(function (response){
console.log(response);
if (response == "ok") {
setTimeout(function() {
return window.location.href = "../drive.php";
}, 3000);
}
else {
$('#error').html('<p class="text-center">'+response+'</p>');
}
});
PHP脚本
session_start();
include '***';
$email = clean($_POST['email']);
$pass = clean($_POST['pass']);
$_SESSION['name'] = $email;
$query = "SELECT * from users WHERE user='$email' and pass='$pass'";
$result = mysql_query($query);
$count = mysql_num_rows($result);
if ($count == 1) {
echo 'ok';
}
else {
echo 'Please Try Again';
};
答案 0 :(得分:0)
php脚本发送回空格和新行,因此即使控制台输出正确的返回字符串,它也不能满足if语句。
我的PHP脚本顶部的include在?&gt;之后有两行额外的空白行。结束标签。删除后,响应现在正确并且满足Ajax调用的if语句。