我真的不知道我的代码在这里有什么问题。来自php的回复没有回到我的ajax调用。这是我的代码。 JQuery的
$(".theform").submit(function(e){
var formData = {
"name": $('#name').val(),
"email": $('#email').val(),
"phone": $('#phone').val(),
"message": $('#message').val()
}
var sendMail = $.ajax({
type: 'POST',
url: 'submit.php',
data: formData,
dataType: 'json',
encode: true
});
sendMail.done(function(output){
if(output == "failed"){
console.log("failed");
)
}
});
e.preventDefault();
});
PHP
if($_POST){
$adminEmail = "";
$data = [];
$to = $adminEmail;
$name = $_POST['name'];
$email = $_POST['email'];
$number = $_POST['phone'];
$headers = "From: $email". "\r\n" .
"Reply-To: $email" . "\r\n" .
'X-Mailer: PHP/' . phpversion();
$message = $_POST['message'];
$sendMail = mail($to, "Mail from $name", $message, $headers);
if($sendMail){
$data = ["success"];
} else {
$data = ["failed"];
}
}
print $data[0];
当我检查请求时,我看到来自php的响应,但我似乎无法在ajax调用中将其打印到屏幕或控制台。 拜托,我做错了什么?顺便说一句,它应该返回"失败"现在