联络表格:405不允许使用方法

问题描述

因此,我已经按照引导摘录网站上的指南进行操作,但似乎仍无法使联系表正常工作。我没有收到提交表单的消息,当我查看控制台时也看到以下消息:无法加载资源:服务器响应状态为405(不允许使用方法

这是我的PHP代码

<?PHP

$from = 'Demo contact form <[email protected]>'


$sendTo = '<[email protected]>';


$subject = 'New message from contact form';

$fields = array('name' => 'Name','surname' => 'Last Name','phone' => 'Phone','email' => 'Email','message' => 'Message'); 

$okMessage = 'Contact form successfully submitted. Thank you,I will get back to you soon!';


$errorMessage = 'There was an error while submitting the form. Please try again later';


error_reporting(E_ALL & ~E_NOTICE);

try
{

    if(count($_POST) == 0) throw new \Exception('Form is empty');
            
    $emailText = "You have a new message from your contact form\n=============================\n";

    foreach ($_POST as $key => $value) {
        // If the field exists in the $fields array,include it in the email 
        if (isset($fields[$key])) {
            $emailText .= "$fields[$key]: $value\n";
        }
    }


    $headers = array('Content-Type: text/plain; charset="UTF-8";','From: ' . $from,'Reply-To: ' . $from,'Return-Path: ' . $from,);
    

    mail($sendTo,$subject,$emailText,implode("\n",$headers));

    $responseArray = array('type' => 'success','message' => $okMessage);
}
catch (\Exception $e)
{
    $responseArray = array('type' => 'danger','message' => $errorMessage);
}



if (!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') {
    $encoded = json_encode($responseArray);

    header('Content-Type: application/json');

    echo $encoded;
}

else {
    echo $responseArray['message'];
}

这是我的JS

$(function () {

 
 
    $('#contact-form').validator();
 
 
    // when the form is submitted
    $('#contact-form').on('submit',function (e) {
 
        // if the validator does not prevent form submit
        if (!e.isDefaultPrevented()) {
            var tigerContact = "contact/TigerContact.PHP";
 
            // POST values in the background the the script URL
            $.ajax({
                type: "POST",url: tigerContact,data: $(this).serialize(),success: function (data)
                {
                    // data = JSON object that contact.PHP returns
 
                    // we recieve the type of the message: success x danger and apply it to the 
                    var messageAlert = 'alert-' + data.type;
                    var messageText = data.message;
 
                    // let's compose Bootstrap alert Box HTML
                    var alertBox = '<div class="alert ' + messageAlert + ' alert-dismissable"><button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button>' + messageText + '</div>';
                    
                    // If we have messageAlert and messageText
                    if (messageAlert && messageText) {
                        // inject the alert to .messages div in our form
                        $('#contact-form').find('.messages').html(alertBox);
                        // empty the form
                        $('#contact-form')[0].reset();
                    }
                }
            });
            return false;
        }
    })
 });

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)