The ajax request is working and access the success function, the problem is when I try to use the data provide by the echo json_encode($data)
.
$data['status']
and $data['error_list']
I having the follow error : Uncaught ReferenceError: data is not defined
Can you guys tell me why it’s happening ?
Controller:
public function get_user()
{
$result = false;
$data["error_list"] = array();
$data["status"] = false;
$email = $this->input->post("email");
$password = $this->input->post("password");
if (empty($email)) {
$data["error_list"] = "O email deve ser preenchido";
} else {
$this->load->model("M_login");
$result = $this->M_login->get_user($email);
}
if($result)
{
if(password_verify($password, $result->password))
{
$this->session->set_userdata("user_id", $result->id);
$this->session->set_userdata("user_name", $result->nome);
$data["status"] = true;
} else {
$data["error_list"]= "Credenciais invalidas";
}
}
else
{
$data["error_list"] = "Credenciais invalias";
}
echo json_encode($data);
}
Ajax request:
$(function(){
// quando ocorrer o submite no form esse evento sera carregado
$("#login-formulario").submit(function(){
//chamando a funcao ajax
$.ajax({
type: "post", //tipo da requisicao
url: BASE_URL + "login/get_user", //url que será chamada
dataType: "JSON",
data: $(this).serialize(),
beforeSend: function(){
clearError();
$("#loading").html(loadingImg());
},
success: function(){
if(data['status'] == true){
clearError();
$("#loading").html(loadingtrue());
}
else{
ShowError(data['error_list']);
}
},
error: function(response){
console.log(response);
}
})
return false;
})
})
3
Answers
Correct your declaration
In Ajax Success Function you didn’t get the response from controller
i Hope this help you