$ .ajax发布到PHP文件抛出405方法是不允许的

时间:2017-05-26 15:41:47

标签: php ajax nginx digital-ocean http-status-code-405

我使用PHP联系表单在Nginx上托管的网站上使用Digital Ocean。我是PHP的新手。我使用这篇文章安装了sendmail - https://www.digitalocean.com/community/questions/php-mail-function-enable。 提交表单错误405方法不允许。 .js和.php文件都在根目录中,所以我不认为这是一个CORS问题。

contact_me.php

<?php
echo $_POST;
if(empty($_POST['name'])                ||
   empty($_POST['email'])               ||
   empty($_POST['message'])     ||
   !filter_var($_POST['email'],FILTER_VALIDATE_EMAIL))
   {
        echo "No arguments Provided!";
        return false;
   }
$name = $_POST['name'];
$email_address = $_POST['email'];
$message = $_POST['message'];
$to = 'mygmailaccount@gmail.com';
$email_subject = "Portfolio Contact:  $name";
$email_body = "Name: $name\n\nEmail: $email_address\n\nMessage:\n$message";
$headers = "From: noreply@example.com" . "\r\n";
$headers .= "Reply-To: $email_address" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
mail($to,$email_subject,$email_body,$headers);
return true;                    
?>

contact_me.js

    $(function() {

    $("input,textarea").jqBootstrapValidation({
        preventSubmit: true,
        submitError: function($form, event, errors) {
            // additional error messages or events
        },
        submitSuccess: function($form, event) {
            console.log('contact_me.js submitSuccess running'); // this prints
            event.preventDefault();
            var name = $("input#name").val();
            var email = $("input#email").val();
            var message = $("textarea#message").val();
            var firstName = name;
            if (firstName.indexOf(' ') >= 0) {
                firstName = name.split(' ').slice(0, -1).join(' ');
            }
            $.ajax({
                url: "http://www.example.com/contact_me.php",
                type: "POST",
                data: {
                    name: name,
                    email: email,
                    message: message
                },
                cache: false,
                success: function() {
                console.log('ajax success function'); // doesn't print
                },
                error: function() {
                console.log('ajax error function'); // this prints
                }
            })
        },
        filter: function() {
            return $(this).is(":visible");
        }
    });

    $("a[data-toggle=\"tab\"]").click(function(e) {
        e.preventDefault();
        $(this).tab("show");
    });
});

我在尝试PHP之前发布到Firebase后端,而ajax在那里工作。任何意见将是有益的。感谢。

0 个答案:

没有答案