1
I’ve been trying to solve this problem for a couple of days, and I saw some questions from this site, but none of them solve the problem. I’m putting Ajax in the login of my TCC, but it will not, no error. I’ve made a file_exists
in the path of the requested file and you’re right. And if you can confirm if I’m getting the data right in PHP, because the examples I saw on the site were only with a value within JSON.
index php.
//coloquei só o script porque o código é longo.. (JQuery está incluso pelo bootstrap..
<script>
function enviar() {
var usuario = $("[name='txtu']").val();
var senha = $("[name='txts']").val();
//está chegando os valores
console.log(usuario +'/'+ senha);
$.ajax({
url: "./controller_php/verificaLogin.php",
type: "POST",
data: {'usuario' : usuario, 'senha' : senha},
dataType: "json"
}).done(function (resposta) {
console.log(resposta);
}).fail(function () {
// está caindo aqui sempre
console.log("Falha");
});
}
</script>
verificaLogin.php
require_once './model_php/login.class.php';
if ($_POST) {
$user = json_decode($_POST['usuario']);
$senha = json_decode($_POST['senha']);
session_start();
//coloquei isso pra testar em uma outra página para ver se estava ocorrendo o post, e na outra página mostra que a variável não foi criada.
$_SESSION['a'] = $json;
if (Login::logar($user, $senha)){
$_SESSION['nome'] = $user;
$cpf = Login::pegaCPFUsuario($user);
$_SESSION['tipoUsuario'] = Login::pegaTipoUsuario($user);
if ($_SESSION['tipoUsuario'] == 2) {
$_SESSION['log'] = 'ativo';
return true;
} else {
$_SESSION['log'] = 'ativoTecnico';
return true;
}
} else {
return false;
}
}
You’ve already tested the line
dataType: "json"
... this line is saying that the return you expect is a JSON, and it seems to me that this is not happening and nor is the case.– Sam
I took it here and it’s still the same..
– Leandro
You’re falling into "fail," maybe you’re not finding your destination
– Sam
Something else,
return
inelse
doesn’t make sense... if the firstreturn
run, it will surely not run the second, so it does not need toelse
. And you’re returning tworeturn true
in the same IF.. ELSE... just onereturn true
after IF. Redundancies that could be avoided.– Sam