找到你要的答案

Q:405 Not Allowed on AJAX Call

Q:405不允许对Ajax调用

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

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

我想做一个使用PHP和AJAX的简单接触形式。当直接调用(通过行动=“sendmail。PHP),php形式的作品很好,但是当我尝试用AJAX我收到405不允许这样做。

现在控制台返回:

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>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;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
}
?>

任何帮助将不胜感激。也有兴趣知道为什么这似乎不工作。

javascript  php  jquery  ajax  http-status-code-405