1

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?

4
  • 1
    Can you maybe show the view code where you want to use the articles response? Commented Dec 20, 2018 at 8:18
  • @Rentius2407 added in the edit portion :) Commented Dec 20, 2018 at 8:44
  • 1
    @ProteetiProva Could you also explain what you are trying to achieve in layman's terms please? Commented Dec 20, 2018 at 10:12
  • @MinarMahmud with my novice knowledge I tried to simplify as much as I could. Edited the question Commented Dec 21, 2018 at 7:37

1 Answer 1

1

I would suggest you to use jQuery plug-in named DataTables. This is a stable tool which I have experience of using in production.

You need to do something like this:

<div class="page-articles-ajax">
    <table id="articleTable">
    </table>
</div>

And in js:

$(function() {
    $('#articleTable').DataTable({
        ajax: '/articles/json'
    });
});

DataTables is a highly customizable plug-in. See it's ajax manual for more.

If you don't want to use this plug-in, you need to build the table with JavaScript after you have the response data from backend. You can use jQuery's strong API to achieve this easily.

Suggestions & Notes:

=> Change to

@GetMapping(value = "/articles/json", produces = MediaType.APPLICATION_JSON_VALUE)

=> produce and consume these terms are used from the perspective of API endpoint/backend.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.