使用 phpmailer 显示 json_encode 警报时出错

Error showing json_encode alert with phpmailer

我的 jquery 文件中有一个函数 post,用于将数据插入数据库并发送一封邮件。在没有 phpmailer 的情况下工作正常,显示警报,但是当我将 phpmailer 代码警报不起作用(但插入数据库并发送电子邮件)时,有人知道为什么会发生这种情况,抱歉英语不好,谢谢帮助。

更新:我已经在网络服务上收到将 json_encode 更改为 return 的警报并更改警报(响应);到 return 警报(响应);在 post 代码上,但需要在关闭 post 方法后发出警报,如果不这样做,return 警报不会出现,有些想法为什么在方法 [=23 之后需要第二个警报=] 得到 return, thx.

我的post代码

$.post('insert',{val: message,name: $("#firstName").val(),desktop_id: $("#desktop_id").val(),permis: $("#permis").val(),phone: $("#phone").val(),mail: $("#mail").val()}, function (response) {
    //feedback
    alert(response);
});

我的网络服务

$app->post('/insert', function(){
require_once('db/dbconnect.php');
$name = $_POST['name'];
$phone= $_POST['phone'];
$phone=(int)$phone;
$mail =$_POST['mail'];
$desktop_id = $_POST['desktop_id'];
$desktop_id=(int)$desktop_id;
$permis=$_POST['permis'];
$date = date("Y-m-d");
$data=array("name"=>$name,"phone"=>$phone,"mail"=>$mail,"desktop_id"=>$desktop_id,"permis"=>$permis,"date"=>$date);
$client=$db->client();
$result = $client->insert($data);
if($result){
$response = 'Success';
}
else{
$response = 'Error';
}
//I remove the variables data
include_once("phpmailer/PHPMailerAutoload.php");

$To = $destinatarios;
$Subject = $assunto;
$Message = $value;
$Host = 'mail.'.substr(strstr($usuario, '@'), 1);
$Username = $usuario;
$Password = $senha;
$Port = "587";
$mail = new PHPMailer();
$mail->CharSet = 'UTF-8';
$body = $Message;

$mail-> IsSMTP(); // telling the class to use SMTP

$mail-> Host = $Host; // SMTP server

$mail-> SMTPDebug = 2; // enables SMTP debug information (for testing)
// 1 = errors and messages
// 2 = messages only

$mail-> SMTPAuth = true; // enable SMTP authentication

$mail-> Port = $Port; // set the SMTP port for the service   server

$mail->addAttachment('mpdf/temp/doc.pdf');

$mail-> Username = $Username; // account username 

$mail-> Password = $Password; // account password

$mail-> SetFrom($usuario, $nomeDestinatario);
$mail-> Subject = $Subject;
$mail-> MsgHTML($body);
$mail-> AddAddress($To, "");
if(!$mail-> Send())
{
}
else 
{
}
echo json_encode($response, JSON_UNESCAPED_UNICODE);
});

当您设置 $response = 'Error: '. print($mail->ErrorInfo);$response = 'Success',然后设置 echo json_encode($response, JSON_UNESCAPED_UNICODE) 时,您看起来像是在尝试将格式不正确的字符串转换为 JSON。