Why my JSON is printed out on screen instead of appearing in DataTable?

Why my JSON is printed out on screen instead of appearing in DataTable?

papus89papus89 Posts: 2Questions: 2Answers: 0
edited November 2015 in Free community support

Ajax

$(document).ready(function() {
        $('#example').dataTable({
            "bJQueryUI" : true,
            "processing" : true,
            "ajax" : "ListAllUserServlet",
                ,
            "columns" : [ {
                "data" : "id"
            }, {
                "data" : "userName"
            }, {
                "data" : "firstName"
            }, {
                "data" : "lastName"
            }, {
                "data" : "email"
            }, {
                "data" : "phone"
            }, {
                "data" : "location"
            }, {
                "data" : "password"
            }, {
                "data" : "gender"
            }, {
                "data" : "birthday"
            } ]
        });
    });

Servlet

    List<UserDTO> users = this.service.getAllUser();
            String json = new Gson().toJson(users); 
            response.setContentType("application/json");
        response.setCharacterEncoding("UTF-8");
        response.getWriter().print(json);

Generated JSON

[{"id":1,"userName":"userName1","firstName":"firstName1","lastName":"lastName1","email":"email1@gmail.com","phone":"36202080085","location":"location1","password":"password1","gender":"m","birthday":"1-02-2015"},
{"id":2,"userName":"userName2","firstName":"firstName2","lastName":"lastName2","email":"email2@gmail.com","phone":"36202080005","location":"location2","password":"password2","gender":"m","birthday":"1-02-2015"},
{"id":3,"userName":"userName3","firstName":"firstName3","lastName":"lastName3","email":"email1@gmail.com","phone":"36202080085","location":"location3","password":"password3","gender":"m","birthday":"1-02-2015"}]

This question has an accepted answers - jump to answer

Answers

  • jLinuxjLinux Posts: 981Questions: 73Answers: 75

    No details, no formatting/syntax highlighting, no instructions to reproduce the error..

    Hopefully you spent more effort in troubleshooting this than you did weiting thia post.

    Just some friendly advice, if youre seeking support, atleast make it easy for people to support .. ;-)

  • allanallan Posts: 63,760Questions: 1Answers: 10,510 Site admin
    Answer ✓

    I've added code highlighting now - the instructions for how to highlight code in Markdown are available here.

    It shows that you have a syntax error on lines 5/6 in the JS code above (two commas in a row).

    Beyond that, we'd need a link to the page, showing the issue, as per the forum rules.

    Allan

This discussion has been closed.