2017-09-13 312 views
-1

我想检查用户电子邮件是否已存在于数据库中。为此我构建了以下代码。内部服务器错误500(codeigniter,ajax)

AJAX代码:

function emailCheck(){ 
    console.log("hello"); 
    var email = $("#email").val();; 
    jQuery.ajax({ 
     type: 'POST', 
     url: '<?php echo base_url(); ?>myCon/customerCheck', 
     data: {email:email}, 
     success:function(response){ 
      $('#test').html(response); 
     } 
    }); 
} 

myCon控制器的功能:

 public function customerCheck(){ 
      $this->load->model('CustomerModel'); 
      $res = $this->customerModel->customerMailCheck(); 
      echo json_encode($res); 
     } 

customerModel模型的功能:

 function customerMailCheck(){ 
      $mail = $this->input->post('email'); 
      $result = $this->db->get_where('privilege_customer', array('email' => $mail); 
      return $result->result(); 
     } 

现在,每当我调用该函数我得到错误,指出内部服务器错误500.

有没有更好的方法来做到这一点?

+1

检查错误日志。 – Script47

+0

您对返回数据进行编码,但是您不会在JavaScript中解码? – Script47

+0

500内部错误意味着你的'controller'文件有'system'错误或者其他一些'error'' –

回答

0

你可以试试这个解决方案为您问题。

请添加下面的代码在你的头

<script type="text/javascript"> 
    base_url = '<?=base_url()?>'; 
</script> 

改变你的控制器功能。

public function customerCheck(){ 
    if ($this->input->is_ajax_request()) { 
     $this->load->model('CustomerModel'); 
     $mail = $this->input->post('email'); 
     $res = $this->customerModel->customerMailCheck($mail); 
     if(!empty($res)) { 
      $data['status'] = 'success'; 
      $data['message'] = 'Email Adress is found'; 
     } else { 
      $data['status'] = 'error'; 
      $data['message'] = 'Data not found'; 

     } 
     echo json_encode($data); 
     exit; 
    } else{ 
     redirect('login/logout'); 
    } 
} 

更改Ajax代码

function emailCheck(){ 
    var email = $("#email").val();; 
    jQuery.ajax({ 
     type: 'POST', 
     url : base_url +'myCon/customerCheck', 
     data: {email:email}, 
     success:function(response){ 
      if(response.status=="success"){ 
       $('#test').html(response.message); 
      } else { 
       console.log(response.message) 
      } 
     } 
    }); 
} 

CustomerModel模型的功能:

function customerMailCheck($mail){ 
    $result = $this->db->get_where('privilege_customer', array('email' => $mail)); 
    return $result->result(); 
} 

我希望该W生病帮助你。

+0

感谢您的修改,但我仍然收到“POST http://[:: 1]/myCon/myCon/customerCheck 500(内部服务器错误)“。 –

+0

请立即检查。 –

+0

没有变化仍然在控制台中出现错误。 –

0

一个支架缺失模型中的功能:

试试这个:

function customerMailCheck(){ 
      $mail = $this->input->post('email'); 
      $result = $this->db->get_where('privilege_customer', array('email' => $mail)); 
      return $result->result(); 
} 
0

您必须在控制器接收ajax请求,并把它传递给

myCon控制器

模型
public function customerCheck(){ 
     $this->load->model('CustomerModel');  
-----> $mail = $this->input->post('email');  ------v 
     $res = $this->customerModel->customerMailCheck($mail); 
      echo json_encode($res); 
     } 

型号:

function customerMailCheck($mail){ 

      $result = $this->db->get_where('privilege_customer', array('email' => $mail); 
      return $result->result(); 
     } 
0

尝试这个

脚本:在这里,你错过了布布尔河Qutes

function emailCheck(){ 
    console.log("hello"); 
    var email = $("#email").val();; 
    jQuery.ajax({ 
     type: 'POST', 
     url: '<?=site_url()?>myCon/customerCheck', 
     data: {"email":email}, 
     success:function(response){ 
      $('#test').html(response); 
     } 
    }); 
} 

型号:在这里,你在$result=线错过)

function customerMailCheck() { 
    $mail = $this->input->post('email'); 
    $result = $this->db->get_where('privilege_customer', array('email' => $mail)); 
    return $result->result(); 
    } 
+0

@akhilregonda在配置中更改$ config ['base_url'] =' ';'to'$ config ['base_url'] ='http://'.$_SERVER [“SERVER_NAME”]。'/ project_name /';' –