I believe there are numerous posts for this question and I have followed all before posting this question here and I am getting
405 (Method Not Allowed) Error
I am wondering what I am actually missing. But really, how to Insert Form Data into PHP file using AngularJs or jQuery AJAX
My scenario is : I have website in GitHub and I the same project within Github I have a .PHP file into which I am inserting Form Submit Data.
angular.module('homeModule').controller('contactController', function ($scope, $routeParams, $location, $http, contactService) {
$scope.submitForm = function (contact) {
if ($scope.ContactForm.$valid) {
contactService.postNewContactHttp(contact)
.then(function (data) {
alert("Success");
}).finally(function (response) {
alert("finally");
});
}
};
});
angular.module('homeModule').factory('contactService', ["$http", "$q", "$resource", function ($http, $q, $resource) {
var postNewContactHttp = function (data) {
var deferred = $q.defer();
$http({
//url: "./scripts/MyScripts/data.php",
url: "https://**.github.io/***/data.php", // Actual PHP Path
method: 'POST',
data: JSON.stringify(data),
headers: {
'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
}
})
.success(function (result) {
deferred.resolve(result);
})
.error(function (result, status) {
deferred.reject(status);
});
return deferred.promise;
};
return {
postNewContactHttp : postNewContactHttp
};
}]);
<form name="ContactForm" id="ContactForm" method="post" role="form" novalidate ng-submit="submitForm(contact)">
.........
</form>
<?php
<!--Below code is in separate PHP File-->
$postdata = file_get_contents("php://input");
$htmlContent = json_decode($postdata);
?>
I have also tried by adding following headers in PHP file but it does not worked as well.
header("Access-Control-Allow-Methods: POST, GET, OPTIONS");
header("Access-Control-Allow-Origin: *");
header("Access-Control-Allow-Headers: Content-Type");
And I always get :- 405 (Method Not Allowed) Error.
Any suggestions will be helpful.
Thanks