Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get Form Post data from Angular in Laravel

I have some issues posting data from my Angular (front) app to my laravel backend (using $resource and factory). Here is my controller that get form data and send them to my factory :

    myApp.controller('EventCtrl', function ($scope, $http, Events) {
    $http.defaults.headers.post["Content-Type"] = "application/x-www-form-urlencoded;charset=utf-8";

    $scope.addEvent = function(){
        postData = {
            'nom' : $scope.eventName,
            'description' : $scope.eventDesc,
            'date' : $scope.eventDate,
            'heure' : $scope.eventHour
        }

        Events.save({},postData).$promise.then(function(result) {
            if(result == 'success') {
                // insert on front
                // redirect to main.html
            } else {
                // refresh
                //$scope.events = Events.getAll();
            }
        });
      };
  });

My factory is as below :

myApp.factory('Events', ['$resource', function($resource) {
    return $resource( 'http://localhost:8888/laravel/public/event/:eventId',
        { eventId: '@eventId' }, {
            get: {
                method: 'GET',
                params: { eventId: '@eventId' },
                isArray: false
            },
            save: {
                method: 'POST',
                params: {},
                isArray: false
            }
        } );
}]);

I can see in my Google Chrome logs that the Json string send is : {"nom":"my name","description":"desc","date":"2014-03-28","heure":"23:00"}: . It seems data sent miss a name (see the " : " at the end of the string), so I cant catch them laravel's side. Did I miss anything to give a name or anything else to be able to get data in my backend?

Thank you for your time !

like image 297
lightalex Avatar asked Feb 20 '26 11:02

lightalex


1 Answers

Actually, I just found a way to do it thanks to this link : http://www.codingswag.com/2013/07/get-raw-post-data-in-laravel/ You can use this code to get all posted data in your routes :

Route::post('/event', function()
{
    // First we fetch the Request instance
    $request = Request::instance();

    // Now we can get the content from it
    $content = $request->getContent();

    var_dump($content);

});
like image 71
lightalex Avatar answered Feb 22 '26 00:02

lightalex



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!