Ajax调用PHP成功触发错误

Ajax调用PHP成功触发错误,php,ajax,email,Php,Ajax,Email,我正在调用一个PHP函数来发送一封包含表单信息的电子邮件,电子邮件被成功发送和接收,当我调试PHP函数时,它返回TRUE。问题是AJAX调用正在捕获[object]错误,它会在AJAX调用的错误部分而不是成功部分触发函数。有人知道这是什么原因吗 以下是我的Java脚本代码: <script type="text/javascript"> $("#candidate_form").submit(function(){ $.ajax({ type: "POST", u

我正在调用一个PHP函数来发送一封包含表单信息的电子邮件,电子邮件被成功发送和接收,当我调试PHP函数时,它返回TRUE。问题是AJAX调用正在捕获[object]错误,它会在AJAX调用的错误部分而不是成功部分触发函数。有人知道这是什么原因吗

以下是我的Java脚本代码:

<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>

$(“#候选人表格”)。提交(函数(){
$.ajax({
类型:“POST”,
url:“php/mail.php”,
数据:{fname:$(“#fname”).val(),lname:$(“#lname”).val(),email:$(“#email”).val(),phone:$(“#phone”).val(),
数据类型:“json”,
成功:功能(响应){
警报(响应)
$(“#jobModal”).show();
},
错误:函数(响应){
警报(“此操作失败,出现以下错误”+响应);
}
})
.完成(功能(数据){
美元(“#jobModal”).modal(“show”);
})
});
以下是我的PHP代码:

<?php


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

function sendCandidate() {

 $emailTo="mail@gmail.com";
 $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: candidates@jobs.com";

 #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     contact@jobs.com with the Subject: Candidate Inquiry.

Regards!

--
Jobs


 ", $headers);





 return json_encode($candidateMailSent);
}



?>

首先,当您进行json_编码时,需要在数组上进行编码。Mail返回一个布尔值,您将其分配给
$candidateMailSent
。您可能应该将其分配给
$candidateSentMail['success']

在mail的php文档中,它表示返回值为true或false。我没有看到它返回消息

然后在jquery端,您将测试响应response.success

例如

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