0

I have a problem, I am a newbie on AngularJS, for a Rest service, I need to authenticate the user but the object is empty.

The first reference to User.mail is empty but on the alert box, I have the real value.

Have you any idea please ?

.factory('Rest', ['$rootScope', '$resource', 'UserService', function($rootScope, $resource, User){
    return $resource($rootScope.services.url + ':serviceId/:itemId/', {}, {
        /** One element **/
        get: {
            method:'GET',
            headers:{
                Authorization: 'Basic ' + Base64.encode(User.mail + ':' + User.password),
                Accept: 'text/xml'
                }, 
            params:{serviceId:'', itemId:''}, 
            transformResponse: function(data) {
                alert('mail : ' + User.mail);
                var response = XmlToJson.init(data);
                if(response.length > 0) 
                    return response[0];/*in this case xmlToJson returns an array with one element*/
                else
                    return {};
            }
        },

1 Answers1

0

Yeah, transformResponse in $resource doesn't actually deserailize JSON into objects (which the docs say it's supposed to do). The object will be available in the transformResponse method, but not when the $resource service returns.

You need to use the $http service and its transformResponse to get the behaviour you're looking for. For example: plunker

For a full explanation see AngularJS transformResponse.

Community
  • 1
  • 1
Chris Clark
  • 1,439
  • 2
  • 17
  • 25