post data - ngResource AngularJS

后端 未结 4 811
你的背包
你的背包 2021-01-24 14:38


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

I\

相关标签:
4条回答
  • 2021-01-24 15:09

    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'))

    0 讨论(0)
  • 2021-01-24 15:10

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

    0 讨论(0)
  • 2021-01-24 15:13

    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.

    0 讨论(0)
  • 2021-01-24 15:14

    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);  
        }  
    });
    
    0 讨论(0)
提交回复
热议问题