Mail data - ngResource AngularJS


Hello!
I am developing a RESTful web application using AngularJS, I am using the ngResource module to send HTTP requests. Web service developed using FuelPHP.

I had a problem creating a resource using the $save ngResource . My web service does not receive email data.

When I test an HTTP request with Firebug, I can see the message data.

I do not understand why the mail data was not received by the web service. Therefore, if you have an idea, it would be great to help me.

Sorry for my poor 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(); } 
+1
source share
4 answers

I believe this is a problem with the way PHP handles POST. When using the AngularJS $ POST resource, an object with JSON will be used as a BODY message. PHP does not consider this a regular parameter. I had to do this in another PHP (never used Fuel)

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

Then you should be able to check $ requestBody as a regular json object.

+1
source

You need to configure the $ save method using the "POST" request method

0
source

Thank you for your responses.
Indeed, data is a message in the body of the request.
With FuelPHP, I used Input :: json ('key') to get the values ​​(and not Input: post ('key'))

0
source

you can set the "transformRequest" parameter to $ http by default to change the formation of the message data transfer.

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

All Articles