【问题标题】:Unable to get the response in jquery after sending email from codeigniter controller从 codeigniter 控制器发送电子邮件后无法在 jquery 中获得响应
【发布时间】:2015-03-22 11:14:54
【问题描述】:

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

我正在发布带有电子邮件功能代码的控制器代码,同时发布我的 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 功能后我没有收到控制器的响应,因此我无法向用户显示确认消息。

【问题讨论】:

    标签: javascript php jquery codeigniter email


    【解决方案1】:

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

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

    Die 对于 http 响应来说不是一个好的做法,因为它可能被解释为一个损坏的页面。为什么不返回 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

    【讨论】:

    • 感谢您的回复,但我仍然得到相同的结果。我对 jquery 部分感到困惑。在 jquery 中,我使用 $(form).serialize() 表单数据发布数据,当我将 contentType: "application/json" 添加到 ajax 时,什么也没有发生。您能否发布我的代码的正确 jquery 部分,以便我能得到正确的响应。这将是一个很大的帮助
    • 啊啊啊……终于得到了回应。我在 ajax 脚本中增加了超时时间,现在得到了正确的响应。感谢您的大力帮助。我会接受您的回复作为答案谢谢
    • 内容类型标头需要在 php 中,以便 jquery 知道其获取 json 并自动将响应转换为 js 对象
    • 我没有这样做,它只能通过增加超时时间来工作
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-05
    • 1970-01-01
    • 2015-12-29
    • 2023-03-27
    • 1970-01-01
    • 2013-08-13
    相关资源
    最近更新 更多