【问题标题】:AngularJS displaying data in json Format.Need it in normal form?AngularJS 以 json 格式显示数据。需要普通格式吗?
【发布时间】:2016-09-23 13:33:56
【问题描述】:

这将如何正常工作?

您好,我创建了一个简单的 Rest 服务:

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

然后使用 Angular 的 MIME 服务和控制器中的 $resource 调用它,如下所示:

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

在控制器中,我通过 Tester Service 调用 REST 服务:

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));
    });

现在在模板{{output}}中打印这个“输出”

它以 json 格式显示 - {"0":"H","1":"e","2":"l","3":"l","4":"o"} 而不是“Hello”。

使用$http 可以工作,但不能使用$resource。我有什么解决办法吗?

提前致谢。

【问题讨论】:

  • 这可能不是角度错误,问题出在服务器上。检查您的服务器设置,例如内容类型或编码。

标签: angularjs json stringify


【解决方案1】:

它看起来像 that 问题的重复。我快速证明了概念,我遇到了同样的问题。你可以简单地将你的信息包装成类似的东西

{
  "message": "Hello"
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-29
    • 2015-06-26
    • 1970-01-01
    • 1970-01-01
    • 2017-12-09
    • 2019-08-11
    相关资源
    最近更新 更多