我在将数据从 Angular(前端)应用程序发布到 Laravel 后端(使用 $resource 和 factory)时遇到了一些问题。
这是我的 Controller ,它获取表单数据并将它们发送到我的工厂:

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

我可以在我的谷歌浏览器日志中看到 Json 字符串发送是:{"nom":"my name","description":"desc","date":"2014-03-28","heure":"23:00"}: 。
似乎发送的数据错过了一个名称(请参阅字符串末尾的“:”),所以我无法捕获他们 laravel 的一面。我是否错过了任何命名或其他任何能够在我的后端获取数据的东西?

感谢您的时间 !

最佳答案

实际上,由于此链接,我刚刚找到了一种方法:http://www.codingswag.com/2013/07/get-raw-post-data-in-laravel/
您可以使用此代码获取 route 的所有发布数据:

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

});

关于angularjs - 在 Laravel 中从 Angular 获取表单发布数据,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/22537464/

10-16 11:27
查看更多