[英]How to return JSON data with angular from a java servlet?
主要目標是使用Java Servlet從Angular返回一些JSON數據。 因此,我有一個工廠DispatcherService
,可在其中獲取我的資源密鑰:
cfdb.tasks.primite.list=/task/received_task.do?method=list&cleanSess=1&json=true
在我的Chrome控制台中,我得到以下信息:
tasks data Resource { SESSION_ATTRIBUTES: Object, REQUEST_PARAMETERS: Object,
REQUEST_ATTRIBUTES: Object, $promise: Promise, $resolved: true} $promise: Promise$resolved: true REQUEST_ATTRIBUTES: Object REQUEST_PARAMETERS: ObjectSESSION_ATTRIBUTES: Object__proto__: Resource
tasksCtrl.js:42
但是我不知道為什么不能從JSON訪問對象。
.factory('DispatcherService', ['$resource', '$location', 'UrlService', function ($resource, $location, UrlService) {
var contextPath = UrlService.getContextPath();
return $resource('', {}, {
getResource: {url: contextPath + '/rest/dispatch', method: 'GET', isArray: false},
getArray: {url: contextPath + '/rest/dispatch', method: 'GET', isArray: true},
invalidateSession: {url: contextPath + '/rest/invalidate/session', method: 'GET', isArray: false},
downloadFile: {url: contextPath + '/rest/download/file', method: 'GET', isArray: false},
postResource: {url: contextPath + '/rest/dispatch', method: 'POST', isArray: false}
});
}])
.controller('TasksCtrl', ['$scope', 'DispatcherService', 'HideHeadersService', '$location', '$log', '$http',
function ($scope, DispatcherService, HideHeadersService, $location, $log, $http) {
DispatcherService.getResource({
key: 'cfdb.tasks.primite.list'
}, function success(data) {
$scope.todos = data; //edited
$log.debug("tasks data",data);
console.log("break and my code: ");
}, function error(data) {
// $scope.pagination.loading = false;
$log.debug("ERROR - getTabResource", data.REQUEST_ATTRIBUTES);
if (data.status && data.status == 401)
$location.path("/login");
});
}])
的HTML
<div ng-controller="TasksCtrl">
<p ng-repeat="task in todos track by $index">{{task.receivedTasksList}}</p> //edited
</div>
好的,現在我看到我無緣無故地用調試消息限制了$scope
: $scope.todos = $log.debug("tasks data",data);
解:
1) $scope.todos = data;
2) <p ng-repeat="task in todos track by $index">{{task.receivedTasksList}}</p>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.