我正在尝试使用 php 从 jquery 发送电子邮件,但出现解析器错误:
索引.html
var myUrl = "http://mysite/mail.php";
$.ajax({
type: "POST",
url: myUrl,
cache: false,
contentType: "application/json; charset=utf-8",
data: { body:"test",
to:"myemail@gmail.com",
subject:"Test Results"
},
dataType: "json",
complete: function(transport, data){
console.log(data);
if (transport.status == 200)
alert("Worked.");
else
alert("Didn't work, " + transport.status);
}
});
邮件.php
<?php
$body = $_POST['body'];
$to = $_POST['to'];
$subject = $_POST['subject'];
mail($to, $subject, $body);
?>
当我放入console.log(data)
索引文件时,我在控制台中看到的结果是“parsererror”。有人知道为什么吗?