最近我开始使用亚马逊的AWS进行网络托管,我的HTML联系表单似乎不再起作用了,点击提交按钮时会出现此错误。
<html> <head><title>405 Method Not Allowed</title></head> <body> <h1>405 Method Not Allowed</h1> <ul> <li>Code: MethodNotAllowed</li> <li>Message: The specified method is not allowed against this resource.</li> <li>Method: POST</li> <li>ResourceType: OBJECT</li> <li>RequestId: A8750F9D0586C1E5</li> <li>HostId: FHjhs//WnJVj8GLE/hKVzkaIBq9DRuhNJxObJ8eARsvAbURpWS87tWivbIsCGnzAeFne9lLLvNI=</li> </ul> <hr/> </body> </html>
我的HTML:
<div class="email-form">
<form id="ajax-contact" method="post" action="mailer.php">
<div class="flex-input-wrap">
<input type="text" placeholder="Name" name="name" id="name" class="flex-input-1" required="">
<input type="email" placeholder="Email" name="email" id="email" class="flex-input-2" required="">
</div>
<textarea type="text" placeholder="Enter text" name="message" id="message" required=""></textarea>
<button type="submit" class="submit-button">Send message</button>
</form>
<div id="form-messages"></div>
</div>
js:
$(function () {
var e = $("#ajax-contact"),
a = $("#form-messages");
$(e).submit(function (s) {
s.preventDefault();
var r = $(e).serialize();
$.ajax({
type: "POST",
url: $(e).attr("action"),
data: r
}).done(function (e) {
$(a).removeClass("error"), $(a).addClass("success"), $(a).text(e), $("#name").val(""), $("#email").val(""), $("#message").val("")
}).fail(function (e) {
$(a).removeClass("success"), $(a).addClass("error"), $(a).text("" !== e.responseText ? e.responseText : "Oops! An error occured and your message could not be sent.")
})
})
});
mailer.php文件:
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$name = strip_tags(trim($_POST["name"]));
$name = str_replace(array("\r","\n"),array(" "," "),$name);
$email = filter_var(trim($_POST["email"]), FILTER_SANITIZE_EMAIL);
$message = trim($_POST["message"]);
if ( empty($name) OR empty($message) OR !filter_var($email, FILTER_VALIDATE_EMAIL)) {
http_response_code(400);
echo "Oops! Did you forget to fill out something? Try again.";
exit;
}
$recipient = "myemail@gmail.com";
$subject = "New email from $name";
$email_content = "Name: $name\n";
$email_content .= "Email: $email\n\n";
$email_content .= "Message:\n$message\n";
$email_headers = "From: $name <$email>";
if (mail($recipient, $subject, $email_content, $email_headers)) {
http_response_code(200);
echo "Thanks! Your message was sent.";
} else {
http_response_code(500);
echo "Oops! Something went wrong and your message couldn't be sent.";
}
} else {
http_response_code(403);
echo "There was a problem with your message, please try again.";
}
?>
我在AWS中缺少某种类型的设置吗?
提前感谢。
答案 0 :(得分:0)
猜测你已经在S3中静态托管了你的index.html,而POST实际上是去了S3,拒绝它,而不是你的PHP服务器。