【发布时间】: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 的一面。我是否错过了任何可以提供名称或其他任何能够在我的后端获取数据的内容?
感谢您的宝贵时间!
【问题讨论】: