1

For start i fetch input from html and search database containing that input, after when i print it in java to check if it works fine, but when i try to console log that same data i get empty object:

Object {  }

My js:

var type = document.getElementById("typeInput");


$('#search').on('click', function(event) {
    var kategorija = type.options[k.selectedIndex].value;

    var json = $("#json");
    console.log(json);

    var params = {
        'type' : type,      
    };


    $.get('ResServlet', params, function(data){

    var json = $("#json");
    console.log(json);


})

my java code:

    String type = request.getParameter("type");
    type = (type != null? type: "");
    System.out.println(kategorija);
//  ServletContext context = getServletContext();

    System.out.println("------------");
    List<Res> ress = ResDAO.getRes(type);
    System.out.print(ress);

    System.out.println("\n");
    System.out.println("+++++++++++");

    String json = new Gson().toJson(ress);

    System.out.print(json);
    request.setAttribute("json", json);
    response.setContentType("application/json");
    response.setCharacterEncoding("UTF-8");
    response.getWriter().write(json);

When i print data with System.out.print(json) i get proper json but when i try to console.log in js i get : Object { } or just Array [] when i to get values from that json. Sorry for noob question i just started using javascript :)

aezakmi
  • 23
  • 5
  • 1
    You can use `console.log(JSON.stringify(json))` and that should output the JSON string in Javascript – Hayes Roach Dec 05 '19 at 17:38
  • 1
    Nope. The problem is that the OP is completely ignoring `data` argument and instead trying to console.log an HTML element with ID `json`. – BalusC Dec 05 '19 at 17:44
  • i just tried console.log(JSON.stringify(json)) but i just got: {} – aezakmi Dec 05 '19 at 17:47

0 Answers0