我正在尝试使用PHP和AJAX创建一个简单的联系表单。当直接调用(通过action="sendmail.php"
)时,php表单工作正常,但是当我尝试使用jquery AJAX时,我收到405 Not Allowed。
现在控制台返回:
n.ajaxTransport.k.cors.a.crossDomain.send @ code.jquery.com/jquery-2.1.4.min.js:4
n.extend.ajax @ code.jquery.com/jquery-2.1.4.min.js:4
n.each.n.(anonymous function) @ code.jquery.com/jquery-2.1.4.min.js:4
n.event.dispatch @ code.jquery.com/jquery-2.1.4.min.js:3
n.event.add.r.handle @ code.jquery.com/jquery-2.1.4.min.js:3
(anonymous function) @ contact.html:187
这似乎暗示了跨域请求,但事实并非如此。该文件是domain.com/contact.html,php文档是domain.com/sendmail.php。
这是原始请求(又名第187行的“匿名函数”)
$(document).ready(function() {
$('#submit').click(function() {
$.post("./sendmail.php", $("#contactform").serialize(), function(data){});
//line 187 ^^^
$('#success').html('<br> Message sent!<br><br>');
$('#success').hide(2000);
});
});
和sendmail.php(编辑:我确实尝试在此处启用错误报告并且没有错误)
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$regarding = $_POST['regarding'];
$comments = $_POST['comments'];
$to = 'myemail@domain.com';
$subject = 'Contact Form';
$message = $name." would like to be contacted regarding " .$regarding. ". " .$name. " can be reached via email at ".$email." or via phone at " .$phone. ".\r\n\r\n S/he also provided the following comments: ".$comments. ".";
$headers = 'From: phpmailer@domain.com';
$mail=mail($to, $subject, $message, $headers);
if($mail){
echo "1"; //success
}
else{
echo "0"; //failed
}
?>
任何帮助将不胜感激。也有兴趣知道为什么这似乎不起作用。