Jquery 如何在codeigniter视图中从json ajax调用获取响应?
在我看来,我试图从codeigniter控制器获得响应,但没有成功。以下是我的javascript文件中的代码:Jquery 如何在codeigniter视图中从json ajax调用获取响应?,jquery,ajax,json,codeigniter,Jquery,Ajax,Json,Codeigniter,在我看来,我试图从codeigniter控制器获得响应,但没有成功。以下是我的javascript文件中的代码: // user enter his e-mail so check him against the database. $("#formSendPassword").submit(function(e){ e.preventDefault(); var email = $(this).find("#checkemail").val(
// user enter his e-mail so check him against the database.
$("#formSendPassword").submit(function(e){
e.preventDefault();
var email = $(this).find("#checkemail").val();
var obj = {email: email};
var url = $(this).attr("action");
var data = {email: email};
$.post(url, obj,data, function(jsonResp){
console.log(success);
if(jsonResp.success) {
alert(jsonResp['success']);
$('#successMailMessage').fadeIn();
} else {
alert("Fail");
$('#errorMailMessage').fadeIn();
}
}, 'json');
})
我的控制器中的代码如下所示:
public function checkEmail()
{
// set the validation rules
$this->form_validation->set_rules('checkemail', 'E-Mail', 'valid_email|required|trim|encode_php_tags');
$this->form_validation->set_error_delimiters('<br /><p class=jsdiserr>', '</p><br />');
// if validation is passed
if ($this->form_validation->run() != FALSE)
{
$ids=array();
$ids[0]=$this->db->where('email', $this->input->post('checkemail'));
$query = $this->backOfficeUsersModel->get();
if($query)
{
$data = array(
'userid' => $query[0]['userid'],
'username' => $query[0]['username'],
'password' => $query[0]['password'],
'firstname' => $query[0]['firstname'],
'lastname' => $query[0]['lastname'],
'email' => $query[0]['email']
);
$jsonResp['success'] = "Ok";
$jsonResp = array();
} else {
// echo json_encode(array("success" => false, "error" => "Wrong email"));
$jsonResp['success'] = "Fail";
}
// form validation has failed
} else {
$errorMessage = "Please enter valid e-mail";
}
} // end of function checkEmail
公共功能检查电子邮件()
{
//设置验证规则
$this->form_validation->set_rules('checkemail','E-Mail','valid_email | required | trim | encode_php_tags');
$this->form\u validation->set\u error\u分隔符(“
”,“
”);
//如果验证通过
如果($this->form\u validation->run()!=FALSE)
{
$ids=array();
$ids[0]=$this->db->where('email',$this->input->post('checkemail');
$query=$this->backOfficeUsersModel->get();
如果($query)
{
$data=数组(
'userid'=>$query[0]['userid'],
'username'=>$query[0]['username'],
'password'=>$query[0]['password'],
'firstname'=>$query[0]['firstname'],
'lastname'=>$query[0]['lastname'],
'email'=>$query[0]['email']
);
$jsonResp['success']=“Ok”;
$jsonResp=array();
}否则{
//echo json_编码(数组(“成功”=>错误,“错误”=>错误电子邮件”);
$jsonResp['success']=“Fail”;
}
//表单验证失败
}否则{
$errorMessage=“请输入有效的电子邮件”;
}
}//功能结束检查电子邮件
如您所见,我正在尝试在我的js文件中记录console.log成功,但没有成功。
谁能告诉我我做错了什么
关于Zoran,在将json响应传递给视图之前,应该使用$data数组元素存储json响应。此视图是javascript回调正在寻找的json响应。我猜你的控制台日志调用永远不会触发 请尝试以下操作: 在控制器代码中,类似于:
$data['json'] = json_encode(array("success" => false, "error" => "Wrong email"));
$this->load->view('json_response', $data);
按如下方式定义视图:
<?php
$this->output->set_header('Content-Type: application/json; charset=utf-8');
echo $json;
?>
$.post(url, data, function(data) {
console.log(data.sucess);
}, "json");
你也不需要通过obj考试。此外,您需要记录data.success,而不仅仅是success。让我知道您的控制器正在设置最终数据,但您没有打印出来。我没有发现您的数组使用json_encode转换为json,然后打印出来 试试看。另外,您正在填充array,为什么还要再次执行=array()
查看此链接是否对您有帮助:为什么您尝试将
obj
和数据
都传递到$。post
?您在哪里使用echo/print
向浏览器进行输出?我尝试在javascript中对输出进行控制台日志记录,但没有得到任何结果。Musa我将它们都添加了,因为它们都没有返回任何结果,所以我添加了echo json_encode($jsonResp);上面两行的吼声,我什么也得不到。console.log仍然为空。没有发生任何事情。我目前正在尝试的是将控制器代码的输出记录到console.log中。我暂时没有收到密码。在添加$data['json']=json_encode(数组(“success”=>false,“error”=>“error email”))之后;我试图在javascript中像这样记录json:console.log(jsonResp);但我什么也没得到,我知道你想从你的控制器上登录——最好的做法还是通过加载我写的视图来实现。我在回答中还添加了特定的jQuery函数,供您参考。
$jsonResp['success'] = "Ok";
$jsonResp = array();