【发布时间】:2018-11-03 02:26:56
【问题描述】:
我正在使用 PHPMailer 发送电子邮件。它工作得很好。问题出在 ajax 调用中。如果我的数据返回“null”,则对话框有效。
我尝试了 if/else if/else 和一个将成功消息设置为默认值的开关。我的控制台没有显示任何反馈,也没有弹出对话框。唯一有效的是电子邮件。
前端:
<body class="text-center">
<div id="incorrect_email_dialog">
<h3>Oops! It looks like you entered an incorrect email</h3>
<h3>Give it another try</h3>
</div>
<div id="correct_email_dialog">
<h3>Please check the email associated with your account</h3>
<h3>for instructions to reset your password</h3>
</div>
<div class="container login-container">
<form id="forgot_pass_form" class="form-signin">
<img class="login_logo" src="css/images/logos/example_logo72x72.png">
<h1 class="font-weight-normal">Please enter email</h1>
<input type="email" id="inputEmail" name="inputEmail" class="form-control" placeholder="Enter email associated with account" required autofocus>
<button id="login_btn" class="btn btn-lg btn-primary btn-block" type="submit">Submit</button>
<div class="register-container">
<p><a class="register-p" href="login.php">Back to Login</a></p>
</div>
</form>
</div>
</body>
<script>
var original_error_dialog = $('#incorrect_email_dialog').html();
$('#forgot_pass_form').submit(function(e) {
e.preventDefault();
console.log(email);
console.log('Submitted form');
$.ajax({
type: 'post',
url: 'includes/token_send.php',
dataType: 'json',
data: $(this).serialize(),
success: function(data) {
console.log(data);
switch(data) {
case 'Error':
console.log('Error');
$('#incorrect_email_dialog').html('<h3>Oops. There was a problem</h3>');
$('#incorrect_email_dialog').dialog('open');
break;
case 'null':
console.log('Data was null');
$('#incorrect_email_dialog').html(original_error_dialog);
$('#incorrect_email_dialog').dialog('open');
break;
default:
$('#correct_email_dialog').dialog('open');
console.log('All good');
break;
}
}
})
});
后端:
<?php
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
use PHPMailer\PHPMailer\Exception;
require_once '../PHPMailer/src/PHPMailer.php';
require_once '../PHPMailer/src/SMTP.php';
include '../../config/DB.php';
if($_SERVER['REQUEST_METHOD'] === 'POST') {
$email = $_POST['inputEmail'];
try { $db = new DB(); } catch (\Exception $e) { $e->getMessage(); }
$result = $db->getRow('SELECT email FROM users WHERE email=?', [$email]);
$input_email = $result['email'];
if($input_email !== null) {
$to = $input_email;
$token = bin2hex(random_bytes(8));
$mail = new PHPMailer();
$mail->isSMTP();
$mail->Host = 'localhost';
$mail->SMTPDebug = 2;
$mail->setFrom('noreply@example.com', '');
$mail->addAddress($to, '');
$mail->Subject = 'Example Password Reset Request';
$mail->isHTML(true);
$mail->Body = '
<h2 style="padding:10px;background:#ec1414;font-size:16px;color:#fff;">Example Password Reset</h2>
<br><br>
<p>Use this link to reset your password</p>
<br>
<a style="text-decoration:none!important;" href="http://example.local/reset.php?token='.$token.'&email='.$input_email.'">Click here to reset</a>';
if(!$mail->send()) {
echo json_encode('Error');
} else {
echo json_encode('Success');
}
} else {
echo json_encode('null');
}
}
我在收件箱中收到了电子邮件,但在“提交的表单”console.log 语句之后没有任何内容。
我也试过了:
case 'Success':
$('#correct_email_dialog').dialog('open');
console.log('All good');
break;
代替default无效。
【问题讨论】:
-
我怀疑您返回的数据比您想象的要多(可能是 PHP 警告通知),因此它不是有效的 JSON,并且由于您指定了
dataType: 'json',因此调用失败。尝试删除dataType: 'json'并查看console.log(data)为您提供什么。 -
尝试删除
json_encode -
console.log(data);显示什么? -
console.log(data) 显示了大量的日志。没有意识到正在发生这种情况。我还删除了 dataType:'json' 并没有改变。
-
您需要删除所有日志内容才能使您的代码正常工作。您可以尝试在后端代码的开头使用
ob_start(),然后在输出JSON之前调用ob_end_clean()。