1


Hello !
I develop a RESTful webapp with AngularJS, I use the ngResource module to send http requests. The webservice is developped with FuelPHP.

I'm having a problem to creating a resource with the $save method of ngResource. My web service doesn't receive post data.

When I check the http request with Firebug, I can see the post data.

I don't understand why the post data are not received by the webservice. So if you have an idea, it would be cool to help me.

Sorry for my bad level in English.

Here is the code :

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 Answers 4

1

I believe this is an issue with how PHP is handling the POST. When using AngularJS $resource it will POST the object with JSON as the post's BODY. PHP does not see this as a regular parameter. I've had to do this in other PHP (never used Fuel)

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

Then you should be able to inspect $requestBody as a normal json object.

Sign up to request clarification or add additional context in comments.

Comments

0

You need to config the $save method with a request method of 'POST'

1 Comment

I think that the default type of the save method is POST. Anyway, I tried it and it doesn't works. app.factory('Medication', ['$resource', 'global', function ($resource, global) { return $resource(global.API+'/medication/medication', {}, { "save": {method:"POST"} } ) }])
0

you can set the default option 'transformRequest' of $http to change the transfer formation of the post data.

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

Comments

0

Thanks for your answers.
Indeed, data is post in the request's body.
With FuelPHP, I used Input::json('key') to get the values (and not Input:post('key'))

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.