2011-10-13 3 views
1

저는 codeigniter의 문의 양식을 통해 이메일을 보내기 위해 ajax를 사용하고 있습니다. 아약스 (JQuery와) 부분은 다음과 같습니다codeigniter 및 ajax : aborted request로 이메일 보내기

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->email->send() 비트를 제거하면 응답이 올바르게 표시되지만 물론 이메일은 전송되지 않습니다.

무엇이 여기에 있습니까?

참고 : CSRF를 사용할 수 있으며 데이터베이스를 쿼리하는 다른 ajax 호출에서 제대로 작동합니다.

+0

당신이 다시 무엇을 얻을 볼 방화범이 끌려와 아약스 요청을 확인 했습니까? – beerwin

+0

@beerwin 예 요청이 중단되고 아무것도 반환하지 않지만 '$ this-> email-> send()'가 없으면 응답이 올바르게 반환됩니다. – fana

+0

이 줄'if (msg.sent) {'바로 뒤에 console.log 또는 경고를 넣고 실제로이 코드를 얻는 지보십시오. – Catfish

답변

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 기능을 사용하고 (기본값) true로 비동기 임대하는 것입니다 시도하는 또 다른 방법. 나는 완전한 기능이 브라우저 잠금없이 기다리고 있다고 생각하지만, 나는 이것의 100 % 확신하지 못한다.

+0

async : false로 설정하면 작동합니다! 그러나 프로세스 중에 브라우저 잠금이 발생합니다 ... 비동기 적으로 작동하지 않는 이유가 있습니까? – fana

+0

누군가가 제발 나를 수정하시기 바랍니다,하지만 난 꽤 확신 왜냐하면 그것은 아약스 비동기이며 자바 스크립트를 반환하지 않고 PHP를 기다리지 않고 실행을 계속합니다. asyn을 false로 설정하면 ajax 함수가 php 함수에서들을 때까지 기다릴 것임을 의미합니다. – Catfish

+0

이것은 올바른 방향으로 나를 밀어 닥쳤다! 문제는 제한 시간 설정입니다. 나는 1000과 그것을 가지고 PHP가 전자 메일을 보내려면 충분하지 않았습니다. 이 값을 더 높은 값 (8000)으로 설정하면이 문제가 해결됩니다! 감사 메기! – fana

1

위의 답변이 작동하지만, 일부는 추가 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