gpt4 book ai didi

javascript - 如何使用 Angular、Spring MVC 显示 JSON 数据?

转载 作者:行者123 更新时间:2023-11-29 21:47:50 26 4
gpt4 key购买 nike

我有通过@RestController 传入的有效 JSON 数据。假设我的配置是正确的,我会以正确的方式在 angular jsp 上显示它吗?

Spring Controller

@RequestMapping(value="/states",
method=RequestMethod.GET,produces= {"application/xml", "application/json"})
@ResponseStatus(HttpStatus.OK)
public Object getStates() throws Exception{

JSONObject obj = new JSONObject();
String result= dataManager.getStates();
obj.put("states", result);
return obj;
}

index.jsp

var app = angular.module('myApp', []);
function MyController($scope, $http){

$scope.getStateDataFromServer = function() {
$http({method: 'GET', url: 'http://localhost:8080/states'}).
success(function(data, status, headers, config) {
$scope.state = data;
}).
error(function(data, status, headers, config)
});
};
};
</script>
</head>
<body>
<div data-ng-app="myApp">
<div data-ng-controller="MyController">
<button data-ng-click="getStateDataFromServer()">Get State data from server</button>
<p>States : {{states}}</p>

最佳答案

你应该解决:

App.js:

var app = angular.module('myApp', []);
//Controller
app.controller("MyController", ["$scope", "$http",function($scope, $http){

$scope.getStateDataFromServer = function() {
$http({method: 'GET', url: 'http://localhost:8080/states'}).
success(function(data, status, headers, config) {
$scope.data = state;
}).
error(function(data, status, headers, config)
});
}
}]);

HTML:

<div data-ng-app="myApp">
<div data-ng-controller="MyController">
<button data-ng-click="getStateDataFromServer()">Get State data from
server</button>

<p>States : {{state}}</p> //Because $scope.data=state;
//If you have data associated should do: {{state.myFirstData}}
</div>
</div>

也许你应该尝试这样的东西:

$http({method: 'GET', url: 'http://localhost:8080/states'}).
success(function(data){ . . .

为了让它更容易,然后在它工作时也放置状态、标题和配置。如果您想处理多个状态,请查找 ng-repeat,我 link一篇对您有帮助的帖子。

为了获取一些 Json,我通常会这样做:

Example.js:

myApp.controller("myController", ["$scope", "$http",function($scope, $http){

$scope.initGetRequest = function(){
$http.get('http://localhost/myJSON.json').success(function(data){

$scope.data=data;
})
}
}]);

希望我对您有所帮助。

关于javascript - 如何使用 Angular、Spring MVC 显示 JSON 数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30466987/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com