Search code examples
angularjsjsonstringify

AngularJS displaying data in json Format.Need it in normal form?


How this will work normally?

Hi I have created a simple Rest service:

@GET
@Path("/SayHello")
@Produces(MediaType.APPLICATION_JSON)
 public String sayhello(){
      String name="Hello";
      return name;
  }

And calling it using $resource from angular's MIME Service and controller like this:

service.factory('Tester', function ($resource) {
    return $resource('http://localhost:8080/Resource/rest/SayHello', {}, {
        test: { method: 'GET',isArray: false ,cache : false  },
     })
});

And in the controller I am calling a REST service through Tester Service:

Tester.test({},function success(response){
        console.log("Tester Success: "+JSON.stringify(response));
        $scope.output=response;
    },
    function error(errorResponse){
        console.log("Tester Error: "+JSON.stringify(errorResponse));
    });

Now on printing this "output" in template {{output}}

It is displaying it in json Format - {"0":"H","1":"e","2":"l","3":"l","4":"o"} rather than 'Hello'.

Using $http it working, but not using $resource. I there any solution?

Thanks in advance.


Solution

  • It looks like duplicate of that question. I made quick proof of concept and I got same issue. Simply you can wrap your message into something like that

    {
      "message": "Hello"
    }