我正在进行简单的登录测试,代码在字段为空时返回json响应,但在登录失败或成功时不返回,如:
请求:
var loader = $('#trabalhando');
$(function() {
$('form').submit(function(e) {
loader.fadeIn("slow");
e.preventDefault();
$.ajax({
url: 'login.php',
data: $(this).serialize(),
method: 'post',
dataType: 'JSON',
success: function(data){
loader.fadeOut("slow");
console.log(data);
alert(data.resp);
},
error: function(data) {
alert(':(');
loader.fadeOut("slow");
console.log(data);
}
});
});
});
响应:
<?php
header('Content-Type: application/json');
if (isset($_POST['cpf']) && isset($_POST['pass']) && $_POST['cpf'] != "" && $_POST['pass'] != "") {
$cpf = $_POST['cpf'];
$passw = sha1(strrev(md5($_POST['pass'])));
include 'config.php';
$sql = "SELECT * FROM users WHERE cpf = :cp AND passwd = :pw";
$chec = $db->prepare($sql);
$chec->bindParam('cp', $cpf, PDO::PARAM_STR);
$chec->bindParam('pw', $passw, PDO::PARAM_STR);
$chec->execute();
if ($chec->rowCount() > 0) {
echo json_encode(array('resp' => 'nice'));
} else {
echo json_encode(array('resp' => 'nope'));
}
} else {
echo json_encode(array('resp' => 'fields'));
}
?>
编辑:更新了代码
答案 0 :(得分:2)
您没有正确绑定参数,因此您可能遇到了一个您未处理的PDO错误。变化:
GlassFish requires Java SE version 6. Your JDK is version 0
要:
$chec->bindParam('cp', $cpf, PDO::PARAM_STR);
$chec->bindParam('pw', $passw, PDO::PARAM_STR);
一般来说,数据库,文件和远程服务器操作(FTP,HTTP,SSH ......)非常挑剔,所以当你依赖这些时,总是进行错误检查!您应该将查询分解为执行正确错误检查的专用函数。
// notice the colon : in front of var names, so it matches the placeholders!
$chec->bindParam(':cp', $cpf, PDO::PARAM_STR);
$chec->bindParam(':pw', $passw, PDO::PARAM_STR);
现在您可以更简单地执行查询:
/**
* @param PDO $db The PDO object with which to perform queries
* @param string $sql raw SQL (eg: "select * from t where a = :param" )
* @param array $params Array of parameter names and values eg: [':param' => 'value']
* @param string $error Will be filled with the error details if the DB operations fail
* @return false|PDOStatement FALSE on error, or the statement on success
*/
function query(PDO $db, $sql, array $params, &$error){
try{
// error check every step!
if(!$stmt = $db->prepare($sql)) throw new Exception($db->errorInfo()[2]);
if(!$stmt->execute($params)) throw new Exception($stmt->errorInfo()[2]);
return $stmt; // return the $stmt for further processing
}catch (Exception $e){
$error = $e->getMessage();
return false;
}
}
你说:
失败与成功,装载机和警报完全相同,但这次在警报中带着悲伤的表情
这是预期的。 Ajax调用中的$stmt = query($db, $sql, $params, $error);
// decide what to do on failure
if(!$stmt) die($error);
// now it's safe to use $stmt to fetch results, count rows...
只意味着服务器正常响应。它没有说明json字符串中的内容。如果您想触发success
Ajax回调,您的服务器需要设置错误Some Updates to Apps Using Google Play services,如下所示:
error
要查找Ajax调用触发的错误的详细信息,请修改回调并检查结果:
http_response_code(401);
echo json_encode(array('resp' => 'nope'));
也许您的服务器正在发送其他内容以及破坏输出的JSON。尝试关闭脚本顶部的缓冲区,然后立即退出echo:
error: function(jqXHR, textStatus, errorThrown){
console.log('textStatus: ' + textStatus);
console.log('errorThrown: ' + errorThrown);
}
答案 1 :(得分:1)
看起来你的config.php文件或你的sql语句有问题
尝试将您的代码放入try catch中,然后将错误返回为json:
<?php
header('Content-Type: application/json');
if (isset($_POST['cpf']) && isset($_POST['pass']) && $_POST['cpf'] != "" && $_POST['pass'] != "")
{
$cpf = $_POST['cpf'];
$passw = sha1(strrev(md5($_POST['pass'])));
try
{
include 'config.php';
$sql = "SELECT * FROM users WHERE cpf = :cp AND passwd = :pw";
$chec = $db->prepare($sql);
$chec->bindParam(':cp', $cpf, PDO::PARAM_STR);
$chec->bindParam(':pw', $passw, PDO::PARAM_STR);
$chec->execute();
if ($chec->rowCount() > 0)
{
echo json_encode(array('resp' => 'nice'));
}
else
{
echo json_encode(array('resp' => 'nope'));
}
}
catch(Exception $e)
{
echo json_encode($e->getMessage());
}
}
else
{
echo json_encode(array('resp' => 'fields'));
}
?>
编辑:合并@ BeetleJuice&#39;