5

I am using a service to update a DB table.

myApp.factory('createGal', function ($http, $q)
{
    return {
        createGal: function ()
        {
            var deferred = $q.defer();
            var newGalleryArray = {};

            newGalleryArray.galleryName = 'New Image Gallery';
            newGalleryArray.client      = 245;

            $http.post('/beta/images/create', {newGalleryArray: newGalleryArray}).success(function(data)
            {
                console.log(data);
                deferred.resolve(data);
            });

            return deferred.promise;
        }
    };
});

PHP

public function create()
{
    print_r($_POST);
}

The array is returning empty. Am i passing the array incorrectly?

Chrome Dev enter image description here

Thanks

SuperNinja
  • 1,538
  • 6
  • 22
  • 45

1 Answers1

6

It's been a while since I've used PHP, but doesn't $_POST just contain request paramaters? $http.post sends data through a JSON payload, not request parameters. So, you'll need to use something like json_decode

dnc253
  • 39,967
  • 41
  • 141
  • 157
  • DOH!! Just had to add $json = json_decode(file_get_contents("php://input")); – SuperNinja Jul 31 '13 at 16:18
  • If you have jQuery on the side send your post data through $.params and add Content-Type 'application/x-www-form-urlencoded' to the request header. This way you can just use the $_POST global. – Oliver Jul 31 '13 at 16:22