【发布时间】:2020-02-20 21:18:07
【问题描述】:
ajax请求正在工作并访问成功功能,问题是当我尝试使用echo json_encode($data)提供的数据时。
$data['status'] 和 $data['error_list']
我有以下错误:Uncaught ReferenceError: data is not defined
你们能告诉我为什么会这样吗?
控制器:
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 请求:
$(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;
})
})
【问题讨论】:
标签: php jquery ajax codeigniter