1


你好 !
我用 AngularJS 开发了一个 RESTful webapp,我使用该ngResource模块发送 http 请求。该网络服务是使用 FuelPHP 开发的。

我在$save使用ngResource. 我的网络服务没有收到帖子数据。

当我用 Firebug 检查 http 请求时,我可以看到 post 数据。

我不明白为什么网络服务没有收到帖子数据。因此,如果您有想法,帮助我会很酷。

对不起我的英语水平不好。

这是代码:

Service : 
app.factory('Medication', ['$resource', 'global', function ($resource, global) {
    return $resource(global.API+'/medication/medication', {}, {})
}])

Method in the controller : 
$scope.addMedication = function() {
    var newMed = new Medication();
    newMed.name = 'nameValue';
    newMed.increaseinr = 1;
    newMed.details = 'detailsValue';
    newMed.$save();
}
4

4 回答 4

1

我相信这是 PHP 如何处理 POST 的问题。使用 AngularJS $resource 时,它​​将使用 JSON 作为帖子的 BODY 发布对象。PHP 不认为这是一个常规参数。我不得不在其他 PHP 中这样做(从未使用过 Fuel)

$requestBody = file_get_contents('php://input');
$requestBody = json_decode($requestBody, true);

然后你应该能够检查 $requestBody 作为一个普通的 json 对象。

于 2014-01-29T19:46:03.407 回答
0

您需要使用“POST”请求方法配置 $save 方法

于 2014-01-29T18:16:33.667 回答
0

感谢您的回答。
实际上,数据已发布在请求的正文中。
使用 FuelPHP,我使用 Input::json('key') 来获取值(而不是 Input:post('key'))

于 2014-02-01T13:05:20.730 回答
0

您可以设置 $http 的默认选项 'transformRequest' 来更改 post 数据的传输格式。

var myApp = angular.module('myApp');  

myApp.config(function ($httpProvider) {  
    $httpProvider.defaults.headers.post["Content-Type"] = "application/x-www-form-urlencoded";
    $httpProvider.defaults.transformRequest = function(data){  
        if (data === undefined) {  
            return data;  
        }  
        return $.param(data);  
    }  
});
于 2014-06-05T15:44:30.110 回答