2015-01-15 4 views
0

Я вызываю функцию PHP для отправки электронной почты с информацией из формы, письма отправляются и получаются успешно, и когда я отлаживаю функцию PHP, она возвращает TRUE. Проблема в том, что вызов AJAX ловит ошибку [object Object] и запускает функцию в разделе ошибки в вызове AJAX вместо успешного. Кто-нибудь знает, что может быть причиной этого?Ajax call to PHP success triggers error

Вот мой код Java Script:

<script type="text/javascript"> 
$("#candidate_form").submit(function(){ 


$.ajax({ 
    type: "POST", 
    url: "php/mail.php", 
    data: { fname: $("#fname").val(), lname: $("#lname").val(), email: $("#email").val(), phone: $("#phone").val()}, 
    dataType: "json", 
    success: function(response){ 
     alert(response) 
     $("#jobModal").show(); 
    }, 
    error: function(response){ 
     alert("This action failed with the following error" + response); 
    } 
    }) 
    .done(function(data) { 
    $("#jobModal").modal("show"); 
    }) 


}); 

</script> 

Вот мой PHP код:

<?php 


if (isset($_POST)) { 
sendCandidate(); 
} 

function sendCandidate() { 

$emailTo="[email protected]"; 
$subject="Candidate Info Submited from Website"; 
$body= "Hello! 

A new candidate has submited his/her information from our website. Please check the candidate´s  information and contact him/her as soon as possible. 

Name: ". $_POST['fname']. " ". $_POST['lname']. 
" 
Email: ".$_POST['email']. 
" 
Phone: ". $_POST['phone']. 
" 

Regards! 

-- 
Jobs 
"; 
$headers="From: [email protected]"; 

#Sending mail with candidate´s info to recruiters. 
$candidateMailSent = mail($emailTo, $subject, $body, $headers); 


#Sending thank you email to candidate. 
mail($_POST['email'], "Jobs - Resume submited", 
"Hello ". $_POST['fname']." ". $_POST['lname']. ","." 

We really appreciate your interest in working with us towards getting your new job. We have received your Resume and Contact Information and one of our Specialized Recruiters  will be getting in touch with you as soon as possible. 

If you have any doubts please feel free to contact us by sending an email to  [email protected] with the Subject: Candidate Inquiry. 

Regards! 

-- 
Jobs 


", $headers); 





return json_encode($candidateMailSent); 
} 



?> 

ответ

0

Прежде всего, когда вы делаете json_encode, это должно быть сделано на массив. Mail возвращает логическое значение, которое вы назначаете $candidateMailSent. Вероятно, вы должны присвоить это $candidateSentMail['success']

в документации php для почты, в которой говорится, что возвращаемое значение истинно или ложно. Я не видел, чтобы оно вернуло сообщение.

Тогда на стороне Jquery вы бы проверить реакцию как responce.success

напр.,

success: function(response.success){ 
     alert(response.success) 
     $("#jobModal").show(); 
    }, 
    error: function(response.success){ 
     alert("This action failed with the following error" + response.success); 
    }