【问题标题】:Send email with codeigniter and ajax: aborted request使用 codeigniter 和 ajax 发送电子邮件:中止请求
【发布时间】:2011-10-13 18:05:28
【问题描述】:

我正在使用 ajax 通过 codeigniter 中的联系表发送电子邮件。 ajax(jquery)部分是:

var dataString = 'nome=' +  nome + '&msg=' + msg + '&email=' + email + '&secure=' + secure + '&mailto=' + mailto + '&ci_token=' + $.cookie("ci_csrfprotection_cookie");

$.ajax({
        url: '<?php echo site_url();?>/contact/send',
        type: 'POST',
        data: dataString,
        timeout: 1000,
        dataType: "json",
        success: function(msg){
                if(msg.sent){
                    $('#feedback').html("<?php echo lang('email_sucesso'); ?>").delay(6000).hide('slow');
                        }
                        else{
                            $('#feedback').html("<?php echo lang('email_erro'); ?>").delay(6000).hide('slow');
                        }
                        botao.attr('disabled', false);
                    }
                });

控制器是:

public function send()
{
    if ($this->input->post('secure') != 'siteform') {
        echo lang('erro_no_js');
    }else{
        $this->load->library('email');

        $nome = $this->input->post('nome');
        $email = $this->input->post('email');
        $msg = $this->input->post('msg');
        $mailto = $this->input->post('mailto');
        $secure = $this->input->post('secure');

        $config['protocol'] = "smtp";
        $config['smtp_host'] = "ssl://smtp.googlemail.com";
        $config['smtp_port'] = "465";
        $config['smtp_user'] = $this->settings['smtp_email'];
        $config['smtp_pass'] = $this->settings['smtp_password'];
        $config['charset'] = "utf-8";
        $config['mailtype'] = "html";
        $config['newline'] = "\r\n";

        $this->email->initialize($config); 

        $this->email->from($email, $nome);
        $this->email->to($mailto);
        $this->email->subject('Contacto do site');
        $this->email->message($msg);
        if ($this->email->send()){
            echo json_encode(array("sent"=>TRUE));
        }else{
            echo json_encode(array("sent"=>FALSE));
        }
    }
}

这实际上正确发送了电子邮件,但 ajax 调用被中止,我再也没有收到消息。

但是,如果我删除 $this-&gt;email-&gt;send() 位,我会得到正确的响应,但当然不会发送电子邮件。

我在这里错过了什么?

注意:我启用了 CSRF,它在其他查询数据库的 ajax 调用中工作正常。

【问题讨论】:

  • 您是否使用 Firebug 检查了您的 ajax 请求,看看您得到了什么?
  • @beerwin 是的,请求被中止并且什么也不返回......但是没有$this-&gt;email-&gt;send(),它会正确返回响应。
  • 尝试在if(msg.sent){这一行之后放置一个console.log或一个警报,看看你的代码是否真的到了这一步。
  • 还可以尝试在 if 语句之前回显 msg.sent 以查看它实际返回的内容。
  • 你检查过你的php错误日志吗?

标签: ajax email codeigniter


【解决方案1】:

尝试像这样将 async 设置为 false。

$.ajax({
        url: '<?php echo site_url();?>/contact/send',
        type: 'POST',
        data: dataString,
        timeout: 1000,
        dataType: "json",
        async: false,
        success: function(msg){
                if(msg.sent){
                    $('#feedback').html("<?php echo lang('email_sucesso'); ?>").delay(6000).hide('slow');
                        }
                        else{
                            $('#feedback').html("<?php echo lang('email_erro'); ?>").delay(6000).hide('slow');
                        }
                        botao.attr('disabled', false);
               }
         });

另一种尝试方法是使用 complete 函数而不是成功函数并将 async 租用为 true(这是默认值)。我认为完整的功能在没有浏览器锁定的情况下等待,但我不能 100% 确定这一点。

【讨论】:

  • async: false 让它工作!但是,在这个过程中我得到了一个浏览器锁定......这有什么原因不能异步工作吗?
  • 如果我错了,请有人纠正我,但我很确定这是因为 ajax 是异步的,并且 javascript 会继续执行而不等待 php 返回一些东西。如果将 asyn 设置为 false,则意味着 ajax 函数将等待直到它从 php 函数中听到。
  • 这让我走上了正轨!问题是超时设置。我有 1000 个,这还不足以让 PHP 发送电子邮件。将其设置为更高的值(8000)可以解决此问题!谢谢鲶鱼!
【解决方案2】:

虽然上述答案有效,但有些人可能会遇到额外的问题,即抛出 php 警告:

Message: date() [function.date]: It is not safe to rely on the system's timezone
settings.

解决此问题的方法是在 php.ini 中设置时区或手动将其添加到 codeigniter index.php。

ini_set('date.timezone', 'America/Chicago');

Date error sending mail using codeigniter

【讨论】:

    猜你喜欢
    • 2017-12-07
    • 2018-03-18
    • 1970-01-01
    • 1970-01-01
    • 2013-07-29
    • 2011-10-02
    • 2013-01-07
    相关资源
    最近更新 更多