【问题标题】:Get Form Post data from Angular in Laravel在 Laravel 中从 Angular 获取表单发布数据
【发布时间】:2014-04-27 13:31:14
【问题描述】:

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

    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 的一面。我是否错过了任何可以提供名称或其他任何能够在我的后端获取数据的内容?

感谢您的宝贵时间!

【问题讨论】:

    标签: angularjs laravel factory


    【解决方案1】:

    实际上,感谢此链接,我刚刚找到了一种方法: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);
    
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-08-27
      • 1970-01-01
      • 2018-12-22
      • 1970-01-01
      • 2015-03-29
      • 2013-09-11
      • 1970-01-01
      相关资源
      最近更新 更多