3

I have a service as

angular.module('inviteService', ['ngResource']).factory('Invite', function ($resource) {
    return $resource('/invite');
});

and my controller is

    $scope.invite = function () {
        console.log("submitting invite for " + $scope.email);

        var invite = new Invite();
        Invite.save({'email': $scope.email}, function (data) {
            console.log('data is: ' + data);
            $scope.message.type = 'info';
            $scope.message.content = data;
//            console.log('controller message' + JSON.stringify($scope.message, null, 2));

            // reset email input box
            $scope.email = undefined;
        });

and relevant directive code as

    scope.$watch('ngModel', function () {
        if (Object.keys(scope.ngModel).length > 0) {
            console.log('directive message: ' + JSON.stringify(scope.ngModel));
            element.show();
            //noinspection JSUnresolvedFunction
            $timeout(function () {
                //element.empty();
                element.fadeOut("slow");
            }, 1000);
        }
    }, true);

When I run the code, I see in Chrome Network tab the response as

"You are already on our invite list"

But the Angular code console shows me

data is: [object Object] notificationController.js:13
directive message:{
"type": "info",
"content": {
    "0": "\"",
    "1": "Y",
    "2": "o",
    "3": "u",
    "4": " ",
    "5": "a",
    "6": "r",
    "7": "e",
    "8": " ",
    "9": "a",
    "10": "l",
    "11": "r",
    "12": "e",
    "13": "a",
    "14": "d",
    "15": "y",
    "16": " ",
    "17": "o",
    "18": "n",
    "19": " ",
    "20": "o",
    "21": "u",
    "22": "r",
    "23": " ",
    "24": "i",
    "25": "n",
    "26": "v",
    "27": "i",
    "28": "t",
    "29": "e",
    "30": " ",
    "31": "l",
    "32": "i",
    "33": "s",
    "34": "t",
    "35": "\""
}

} Why is that data is not coming as string?

meda
  • 45,103
  • 14
  • 92
  • 122
daydreamer
  • 87,243
  • 191
  • 450
  • 722
  • What does `console.log(data);` output? – zs2020 Aug 27 '13 at 03:48
  • data is: {"0":"\"","1":"Y","2":"o","3":"u","4":" ","5":"a","6":"r","7":"e","8":" ","9":"a","10":"l","11":"r","12":"e","13":"a","14":"d","15":"y","16":" ","17":"o","18":"n","19":" ","20":"o","21":"u","22":"r","23":" ","24":"i","25":"n","26":"v","27":"i","28":"t","29":"e","30":" ","31":"l","32":"i","33":"s","34":"t","35":"\""} – daydreamer Aug 27 '13 at 03:49
  • Can you try request with text/plain in header like this `$httpProvider.defaults.headers.common['Accept'] = 'text/plain';`? – zs2020 Aug 27 '13 at 03:54
  • where do you want me to try it? – daydreamer Aug 27 '13 at 03:54
  • `app.config(function($httpProvider) {$httpProvider.defaults.headers.common['Accept'] = 'text/plain';});` – zs2020 Aug 27 '13 at 03:56

1 Answers1

2

ngResource expects an object or an array of objects in the response from the server.

See https://stackoverflow.com/a/13816008/215945 for your options:

  1. use $http instead of $resource
  2. return an object from your server (probably the easiest approach, if you can modify the server):
    { "str": "'You are...'"}
  3. intercept and modify the returned value (probably too much work)
Community
  • 1
  • 1
Mark Rajcok
  • 362,217
  • 114
  • 495
  • 492