所以我试图在Angular中设置一个简单的反馈表单,它将在后端使用php发送电子邮件。问题是我一直收到以下错误:
POST https://localhost/mailer/contact-form.php 405 (Method Not Allowed)
405 Method Not Allowed
The method POST is not allowed for this resource.
You cannot POST a file
我认为它与CORS有关,但不知道它是否与HTTPS更相关,因为我们正在运行https://localhost进行开发。
我认为这是控制器的重要部分:
testControllers.controller('FeedbackCtrl', ['$scope', '$http', function ($scope, $http) {
$scope.feedback_errors = "";
$scope.hasError = false;
$scope.resultMessage;
$scope.formData = {};
$scope.submitButtonDisabled = false;
$scope.submitted = false; //used so that form errors are shown only after the form has been submitted
var param = function(data) {
var returnString = '';
for (var d in data){
if (data.hasOwnProperty(d))
returnString += d + '=' + data[d] + '&';
}
// Remove last ampersand and return
return returnString.slice( 0, returnString.length - 1 );
};
$scope.submit = function(feedbackForm) {
$scope.submitted = true;
$scope.submitButtonDisabled = true;
if (feedbackForm.$valid) {
$http({
method : 'POST',
url : 'mailer/contact-form.php',
data : param($scope.formData),
headers : { 'Content-Type': 'application/x-www-form-urlencoded' }
}).success(function(data){
console.log(data);
if (data.success) {
$scope.submitButtonDisabled = true;
$scope.resultMessage = data.message;
} else {
$scope.submitButtonDisabled = false;
$scope.resultMessage = data.message;
}
});
} else {
$scope.submitButtonDisabled = false;
}
}
}]);
这是php:
<?php
header('Access-Control-Allow-Origin: https://localhost');
header('Access-Control-Allow-Methods: GET, POST, OPTIONS');
$errors = array();
$data = array();
if (empty($_POST['comments']))
$errors['comments'] = 'Feedback is required.';
if ( ! empty($errors)) {
$data['success'] = false;
$data['errors'] = $errors;
$data['messageError'] = 'Please check the fields in red';
} else {
$data['success'] = true;
$data['messageSuccess'] = 'Thanks for providing valuable feedback to the VIVA team.';
$email_to = "jknowles@intellisis.com";
$email_subject = "Feedback from VIVA webapp";
$name = $_POST['firstname'] . ' ' . $_POST['lastname'];
$email_from = $_POST['email'];
$message = $_POST['comments']; // required
$email_message = "Form details below.nn";
$email_message .= "Name: ".$name."n";
$email_message .= "Email: ".$email_from."n";
$email_message .= "Message: ".$message."n";
$headers = 'From: '.$email_from."rn".
'Reply-To: '.$email_from."rn" .
'X-Mailer: PHP/' . phpversion();
@mail($email_to, $email_subject, $email_message, $headers);
}
// return all our data to an AJAX call
echo json_encode($data);
?>
我已经为php文件的标题尝试了很多不同的组合,我想知道的一件事是我在前端丢失了什么吗?任何帮助都会受到极大的赞赏,因为我已经把头撞到了墙上好几个小时。
最诚挚的问候, 朱莉
修改:
所以我使用Angular创建了一个简单的表单,然后在我自己的服务器上运行它,它运行正常。
以下是有效工作和无效工作的响应标题:
DOESN&#39; WORK
General:
Remote Address:xx.xx.xx.xx:yyy
Request URL:https://xx.xx.xx.xx/form_test/processForm.php
Request Method:POST
Status Code:405 Not Allowed
Response Headers
Connection:keep-alive
Content-Length:574
Content-Type:text/html
Date:Mon, 06 Apr 2015 18:44:20 GMT
Server:nginx/1.6.2
WORKS
General:
Remote Address:xx.xx.xx.xx:yyy
Request URL:http://juliemarie.me/form_test/processForm.php
Request Method:POST
Status Code:200 OK
Response Headers
Access-Control-Allow-Methods:GET, POST, OPTIONS
Access-Control-Allow-Origin:*
Connection:Keep-Alive
Content-Type:text/html
Date:Mon, 06 Apr 2015 18:37:19 GMT
Keep-Alive:timeout=15, max=100
Server:Apache
Transfer-Encoding:chunked
似乎甚至没有访问过php文件。这可能吗?