I've consumed a json response from controller with ajax which looks like this:
controller:
@GetMapping("/articles/json")
@ResponseBody
public List<Article> viewArticle(Map<String, Object> viewBag) {
Iterable<Article> articles = articleRepository.findAll();
for (Article article:articles) {
articles1.add(article);
}
return articles1;
}
@GetMapping("/articles/ajax")
public String viewArticlesAjax(Map<String, Object> viewBag) {
return "articles/list-ajax";
}
articles.js looks like this
(function () {
if($('.page-articles-ajax').length > 0){
console.log("Yea Im working",$.get);
$.get( "/articles/json", function( data ) {
console.log('got data',data);
if(data && data.length){
for(var i = 0;i < data.length; i++){
var article = data[i];
console.log("article",article);
}
}
});
}
})();
list-ajax.html looks like this:
<html xmlns:th="http://www.thymeleaf.org"
th:replace="~{fragments/layout :: layout (~{::body},'owners')}">
<body>
<h1>Articles</h1>
<div class="page-articles-ajax">
</div>
</body>
</html>
in articles/json ,the json array is returned (here it is).I want to show the json response in a table format where the columns would be "title", "description" and "author". Being new to Spring and MVC I am facing difficulties.
Any help with this?