I use AngularJS to build an AJAX form which is then processed on a server running Wordpress.
Server-side handler is simple:
function rnr_contact_callback() {
$name = $_POST['firstName'] . ' ' . $_POST['lastName'];
wp_mail(
'[email protected]',
'Contact form submitted',
$name . '(' . $_POST['email'] . ') sent a message: ' . $_POST['comment']
);
exit;
}
Client-side controller:
angular.module('app').controller('ContactForm', function($scope, $http) {
$scope.sendContactForm = function() {
$http({
method: 'POST',
url: '/wp-admin/admin-ajax.php',
params: {
action: 'contact',
firstName: $scope.userFirstName,
lastName: $scope.userLastName,
email: $scope.userEmail,
comment: $scope.userComment
}
}).success(function(data, status, headers, config) {
$scope.contactFormSent = true;
}).error(function(data, status, headers, config) {
});
};
});
I tested the $scope
consistency, and it is all right. Moreover, the POST request is handled: I receive an email. The problem is, email body looks like: () sent a message:
. I make a conclusion that, on server, none of $_POST['...']
is set. Why? What do I do wrong?
curl -X POST http://104.131.108.xx/wp-admin/admin-ajax.php --data '{"action":"rate","shop":1,"rating":5}' --header "Content-Type: application/json"
returns 401 Unauthorized. I'm completely sure I allowed unauthorized users to send POST requests.