AngularJS with ngResource: How to check exact response from server?

后端 未结 2 993
礼貌的吻别
礼貌的吻别 2021-02-14 01:22

I have created an angular service to talk back to a simple REST server made in PHP. I can see that I am able to get a single record, list of all records, and add new records, ho

2条回答
  •  野趣味
    野趣味 (楼主)
    2021-02-14 01:54

    See this fiddle: http://jsfiddle.net/moderndegree/Kn3Tc/

    angular.module('myApp', ['ngResource']).
    factory('myService', function($http, $resource, $log){
        return $resource('/', {}, {
            get: {
                method: 'GET',
                transformRequest: [function(data, headersGetter){
                    // you can examine the raw request in here
                    $log.info(data);
                    $log.info(headersGetter());
                }].concat($http.defaults.transformRequest),
                transformResponse: [function (data, headersGetter) {
                    // you can examine the raw response in here
                    $log.info(data);
                    $log.info(headersGetter());
                    return {tada:"Check your console"};
                }].concat($http.defaults.transformResponse)
            }
        });
    }).
    controller('myController', function(myService, $scope, $resource, $http, $log) {
        $scope.results = myService.get();
    });
    

    To globally augment or override the default transforms, modify the $httpProvider.defaults.transformRequest and $httpProvider.defaults.transformResponse properties.

    read more here: http://docs.angularjs.org/api/ng.$http

提交回复
热议问题