drn5375 2015-10-08 21:10
浏览 45

AJAX呼叫时不允许405

I am trying to make a simple contact form using PHP and AJAX. When called directly (via action="sendmail.php"), the php form works fine, however when i try to do it with jquery AJAX i receive 405 Not Allowed.

Now the console returns:

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

This seems to suggest a cross domain request, except it's not. The file is domain.com/contact.html, and the php document is domain.com/sendmail.php.

Here is the original request (aka the "anonymous function" on line 187)

$(document).ready(function() {
        $('#submit').click(function() {
          $.post("./sendmail.php", $("#contactform").serialize(), function(data){}); 
          //line 187 ^^^
            $('#success').html('<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Message sent!<br><br>');
            $('#success').hide(2000);
        });
    });

and sendmail.php (Edit: I did try turning on error reporting here and got no errors)

<?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. ".

 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
}
?>

any help would be appreciated. would also be interested to know why this doesn't seem to be working.

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 Vue3 大型图片数据拖动排序
    • ¥15 划分vlan后不通了
    • ¥15 GDI处理通道视频时总是带有白色锯齿
    • ¥20 用雷电模拟器安装百达屋apk一直闪退
    • ¥15 算能科技20240506咨询(拒绝大模型回答)
    • ¥15 自适应 AR 模型 参数估计Matlab程序
    • ¥100 角动量包络面如何用MATLAB绘制
    • ¥15 merge函数占用内存过大
    • ¥15 使用EMD去噪处理RML2016数据集时候的原理
    • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大