从 codeigniter 控制器发送电子邮件后无法在 jquery 中获得响应

Unable to get the response in jquery after sending email from codeigniter controller

在将发送电子邮件功能添加到控制器后我没有收到回复,但是代码完美地发送了电子邮件,唯一的问题是没有收到回复。如果我删除发送电子邮件的代码,那么在这种情况下,控制器的响应很好,但是当我包含电子邮件功能时,我没有在 jquery 查看代码上收到响应。

我正在 post我的控制器代码和电子邮件功能代码 post 我的 jQuery 以便您可以查看它。

控制器代码: /*******创建新客户*******/

public function createcustomer()
    {
        $this->form_validation->set_rules("firstname", "First Name", "required");
        $this->form_validation->set_rules("lastname", "Last Name", "required");
        $this->form_validation->set_rules("email", "Email", "required|is_unique[gab_users.email]|trim|xss_clean");
        $this->form_validation->set_rules('password', 'Password', 'required|min_length[5]|matches[confirmpassword]|md5');
        $this->form_validation->set_rules('confirmpassword', 'Password Confirmation', 'required|min_length[5]');

    if($this->form_validation->run() === FALSE)
    {
        die("fail");
    }
    else
    {
        $activation_code = sha1(uniqid($this->input->post('email'),'true'));
        $data = array("firstname"=>$this->input->post('firstname'), "lastname"=>$this->input->post('lastname'), "email"=>$this->input->post('email'), "password"=>$this->input->post('password'), "ip"=>$this->input->post('ip'), "activation_code"=>$activation_code, "created_on"=>now(), "active"=>1, "group"=>2);
        $verifyCreate = $this->index_model->set_user($data);

        if($verifyCreate === "success")
        {               

                if($this->sendEmail() === 'done')//If I remove this line and write only die('done'); its work perfectly. model is working fine data is saved in the database in both conditions. The big issue is response?
                {die('done');}
        }
        else
        {
            die("invalid");
        }

    }

}
**/******Send Email********/**
public function sendEmail()
{
      $this->load->library('email'); 
      $this->email->set_newline("\r\n");
      $this->email->from('email@gmail.com', 'Name');
      $this->email->to('*****@gmail.com');
      $this->email->subject(' My mail through codeigniter from localhost '); 
      $this->email->message('Hello World…');
      if (!$this->email->send()) {
        die(show_error($this->email->print_debugger())); 
        }
      else {
        die("done"); //I have also check it by return same thing is happened
      }
}

我的Jquery代码写在视图端:

<script>$().ready(function() {
    $("#regForm").validate({
        rules: 
        {
            firstname: "required",
            lastname: "required",
            email: 
            {
                required: true,
                email: true
            },
            password: 
            {
                required: true,
                minlength: 5
            },
            confirmpassword: 
            {
                required: true,
                minlength: 5,
                equalTo: "#password"
            }
        },
        messages: 
        {
            firstname: "First Name Required",
            lastname: "Last Name required",
            email: "Please enter a valid email address",
            password: 
            {
                required: "Please provide a password",
                minlength: "Your password must be at least 5 characters long"
            },
            confirmpassword: 
            {
                required: "Please provide a password confirmation",
                minlength: "Your password must be at least 5 characters long",
                equalTo: "Please enter the same password as above"
            }
        },
        submitHandler: function(form) 
        {alert('customer register');
          $.ajax({
            type: "POST",
            url: "<?php echo base_url(); ?>createcustomer",
            data: $(form).serialize(),
            timeout: 3000,
            success: function(data) {
            alert(data);
                if(data == 'done')
                {
                    $.blockUI({ css: {
                                border: 'none',
                                padding: '15px',
                                backgroundColor: '#000',
                                '-webkit-border-radius': '10px',
                                '-moz-border-radius': '10px',
                                opacity: .5,
                                color: '#fff'
                            } });

                            setTimeout($.unblockUI, 2000);
                    $('#successreg').show().delay(7000).fadeOut();
                    window.setTimeout(function(){
                        window.location.href = "<?php echo base_url(); ?>customer";
                    }, 7000);

                }
                else if(data == 'invalid')
                {
                   $.blockUI({ css: {
                                border: 'none',
                                padding: '15px',
                                backgroundColor: '#000',
                                '-webkit-border-radius': '10px',
                                '-moz-border-radius': '10px',
                                opacity: .5,
                                color: '#fff'
                            } });

                            setTimeout($.unblockUI, 2000);
                            $('#regerrormsg').show().delay(7000).fadeOut();
                    window.setTimeout(function(){
                        window.location.href = "<?php echo base_url(); ?>customer";
                    }, 7000);
                }
                else
                {
                   $.blockUI({ css: {
                                border: 'none',
                                padding: '15px',
                                backgroundColor: '#000',
                                '-webkit-border-radius': '10px',
                                '-moz-border-radius': '10px',
                                opacity: .5,
                                color: '#fff'
                            } });

                            setTimeout($.unblockUI, 2000);
                    $('#regerrormsg').show().delay(7000).fadeOut();
                    window.setTimeout(function(){
                        window.location.href = "<?php echo base_url(); ?>customer";
                    }, 7000);
                }
            }
          });
          return false;
        }

    });

}); </script>

请告知为什么我在发送电子邮件并将数据保存到数据库时使用 sendEmail 函数后没有收到控制器的响应,但没有收到响应,所以我无法向用户显示确认消息。

编辑:请注意以下所有内容也是好东西,但您的实际问题是您如何从 sendMail() 函数中 returning。 Die 是错误的,因为它会杀死脚本(名称中有线索),因此它不会继续允许调用脚本响应。更改为:

 ....
 if (!$this->email->send()) {
    die(show_error($this->email->print_debugger())); 
    }
  else {
    return "done"; 
  }

对于 HTTP 响应来说,Die 不是一个好的做法,因为它可能被解释为损坏的页面。为什么不 return jQuery 喜欢的东西,例如 json 字符串。例如:

header('Content-Type: application/json');
if($verifyCreate === "success")
    {               

            if($this->sendEmail() === 'done'){
                 echo json_encode(array('status'=>'success'));
                 return;
            }
    }
    else
    {
        echo json_encode(array('status'=>'fail'));
        return;
    }

然后在你的 jQuery

    //redacted
    success: function(data) {
         if(data.status==='success'){
              //do some success stuff
         }
         else if(data.status==='fail'){
              //do some fail stuff
         }else{
              //do some unexpected stuff
         }
    }

您可能还需要在 $.ajax 对象中设置 contentType:json