发送邮件后的PHP/jQuery AJAX错误响应
我正在用PHP编写一个邮件发送器,并使用jQuery通过ajax调用它。这是我的PHP代码的一部分:发送邮件后的PHP/jQuery AJAX错误响应,php,jquery,ajax,Php,Jquery,Ajax,我正在用PHP编写一个邮件发送器,并使用jQuery通过ajax调用它。这是我的PHP代码的一部分: // Send the email. if (mail($recipient, $subject, $email_content, $email_headers)) { // Set a 200 (okay) response code. http_response_code(200); echo "
// Send the email.
if (mail($recipient, $subject, $email_content, $email_headers)) {
// Set a 200 (okay) response code.
http_response_code(200);
echo "Thank You! Your message has been sent.";
} else {
// Set a 500 (internal server error) response code.
http_response_code(500);
echo "Oops! Something went wrong and we couldn't send your message.";
}
这是我的JS:
$(form).submit(function(e) {
// Stop the browser from submitting the form.
e.preventDefault();
// Serialize the form data.
var formData = $(form).serialize();
// Submit the form using AJAX.
$.ajax({
type: 'POST',
url: $(form).attr('action'),
data: formData
})
.done(function(response) {
// Make sure that the formMessages div has the 'success' class.
$(formMessages).removeClass('error');
$(formMessages).addClass('success');
// Set the message text.
$(formMessages).text(response);
// Clear the form.
$('#nombre').val('');
$('#email').val('');
$('#telefono').val('');
$('#pais').val('');
})
.fail(function(data) {
// Make sure that the formMessages div has the 'error' class.
$(formMessages).removeClass('success');
$(formMessages).addClass('success');
// Set the message text.
if (data.responseText !== '') {
$(formMessages).text(data.responseText);
} else {
$(formMessages).text('Oops! An error occured and your message could not be sent.');
}
});
});
以及HTML表单,以防万一:
<form id="ajax-contact" method="post" action="mailer.php">
<ul>
<li><input name="nombre" id="nombre" class="inputNombre" placeholder="Nombre" required/></li>
<li><input name="email" id="email" placeholder="E mail" type="email" class="inputEmail" required/></li>
<li><input name="telefono" placeholder="Teléfono" class="inputTelefono" id="telefono" required/></li>
<li><input name="pais" placeholder="País" class="inputPais" id="pais" required /></li>
<li><input name="submit" type="submit" value="Enviar" class="botonEnviar" id="submit"></input></li>
</ul>
</form>
奇怪的是,即使邮件已发送,它也总是转到else
分支并设置500代码,因此当我试图管理响应时,我得到了错误的响应。但是我收到了邮件,所以if
正在工作
我总是得到回复代码500,应该是邮件失败时设置的
我做错了什么?尝试添加elseif(!)
尝试添加elseif(!)
检查您的web主机的PHP版本。只能从PHP5.4进行设置。
编辑:我今天遇到了完全相同的问题。邮件发出后,PHP返回500条回复。我试着在另一台装有PHP5.5.21的服务器上运行代码,它运行得非常完美。检查您的web主机的PHP版本。只能从PHP5.4进行设置。
编辑:我今天遇到了完全相同的问题。邮件发出后,PHP返回500条回复。我尝试在另一台服务器上运行PHP 5.5.21版本的代码,它运行得非常完美。您标记了AJAX,但没有任何javascript代码。您应该添加更多的代码,因为您发布的代码是有效的。我添加了更多的代码来澄清,对不起。请查看PHP错误日志。
mailer.PHP
?您是否确定500个错误来自其他
,而不是来自代码中的其他地方?我会添加错误报告(E_ALL)
到文件的开头,然后直接从浏览器调用页面(如果可能,使用GET),查看文件中是否存在可能导致此问题的任何其他错误您标记了AJAX,但没有任何javascript代码。您应该添加更多的代码,因为您发布的代码是有效的。我添加了更多的代码来澄清,对不起。请查看PHP错误日志。mailer.PHP
?您是否确定500个错误来自其他
,而不是来自代码中的其他地方?我会添加错误报告(E_ALL)
到文件开头,然后直接从浏览器调用页面(如果可能,使用GET),查看文件中是否存在可能导致此问题的任何其他错误
if (mail($recipient, $subject, $email_content, $email_headers)) {
// Set a 200 (okay) response code.
http_response_code(200);
echo "Thank You! Your message has been sent.";
}
elseif (!mail($recipient, $subject, $email_content, $email_headers)) {
// Set a 500 (internal server error) response code.
http_response_code(500);
echo "Oops! Something went wrong and we couldn't send your message.";
}