从Angular中获取表单Post数据到Laravel

3

我在使用Angular(前端)应用程序向我的Laravel后端(使用$resource和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();
            }
        });
      };
  });

我的工厂如下:

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
            }
        } );
}]);

我可以在我的Google Chrome日志中看到发送的Json字符串是: {"nom":"my name","description":"desc","date":"2014-03-28","heure":"23:00"}:。 似乎发送的数据缺少一个名称(请注意字符串末尾的“:”),因此我无法在Laravel端捕获它们。我是否漏掉了任何内容来指定名称或其他内容以便能够在后端获取数据?
谢谢您的时间!
1个回答

3

实际上,我刚刚在这个链接中找到了一种方法:http://www.codingswag.com/2013/07/get-raw-post-data-in-laravel/。你可以使用以下代码在你的路由中获取所有已发布的数据:

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);

});

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接