Javascript AngularJS未正确处理JSON
我有一个Play框架项目,它的视图使用AngularJS。负责查询数据的控制器发出2个请求,每个请求返回一个JSON块。第一个工作正常,显示正确。下面的例子几乎破坏了第二个人的数据 JSON在呈现之前已正确创建,如应用程序日志所示。 这是从Play Framework routed方法的日志中获取的正确JSON:Javascript AngularJS未正确处理JSON,javascript,json,angularjs,playframework,Javascript,Json,Angularjs,Playframework,我有一个Play框架项目,它的视图使用AngularJS。负责查询数据的控制器发出2个请求,每个请求返回一个JSON块。第一个工作正常,显示正确。下面的例子几乎破坏了第二个人的数据 JSON在呈现之前已正确创建,如应用程序日志所示。 这是从Play Framework routed方法的日志中获取的正确JSON: {"id":5,"name":"auditoria","url":null,"version":1,"methods":[]} 这就是AngularJS打印它的方式。它表示: [{}
{"id":5,"name":"auditoria","url":null,"version":1,"methods":[]}
这就是AngularJS打印它的方式。它表示:
[{},{"0":"a","1":"u","2":"d","3":"i","4":"t","5":"o","6":"r","7":"i","8":"a"},{},{},{"length":0}]
这是控制器:
app.controller("ViewCtrl", [ "$scope", "$resource", "$routeParams", "apiUrl",
function($scope, $resource, $routeParams, apiUrl) {
var ServiceList = $resource(apiUrl + "/services");
$scope.services = ServiceList.query(); //JSON is displayed properly
if ($routeParams.id) {
jsonServicoQuery = apiUrl + "/services/" + $routeParams.id
var Service = $resource(jsonServicoQuery);
$scope.currentService = Service.query(); //JSON is butchered
}
} ]);
以下是HTML:
<div class="row">
<div class="col-md-3">
<div class="bs-sidebar hidden-print" role="complementary">
<ul class="nav bs-sidenav">
<li><a href="/create"><i class="fa fa-plus"></i></a></li>
<li ng-repeat="s in services| orderBy:'name'"><a
href="/view/{{s.id}}">{{s.nome}}</a>
<ul>
<li ng-repeat="m in s.methods| orderBy:'name'">{{m.name}}
[{{m.id}}]</li>
</ul></li>
</ul>
</div>
</div>
<div class="col-md-9" role="main">
<div class="bs-docs-section">
<div class="page-header">
<!-- displaying the whole currentService JSON for debugging purposes -->
{{currentService}}
</div>
</div>
</div>
</div>
和controllers.Services.showid:字符串实现:
public static Result show(String id) {
Service s = Service.findById(id);
//JSON displayed here is correct, log below
Logger.info("At Services show, json " + Json.toJson(s));
return ok(Json.toJson(s));
}
日志:
我设法找到了问题,我应该使用$scope.currentService=Service.get;而不是$scope.currentService=Service.query 看起来你的服务有问题。你能发布你的服务内容吗?
public static Result show(String id) {
Service s = Service.findById(id);
//JSON displayed here is correct, log below
Logger.info("At Services show, json " + Json.toJson(s));
return ok(Json.toJson(s));
}
[info] application - At Services show, json {"id":5,"name":"auditoria","url":null,"version":1,"methods":[]}