Empty POST data on server on AJAX request using Angular $http

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?

4 Answers
4

I got it. I just should have used $_REQUEST instead of $_POST within every function which handles AJAX requests.

Leave a Comment