jquery - How to use json array in ajax ? (java) -


this question has answer here:

i have servlet returns json data :

{"type1":"value1","type2":"value2"}{"type1":"value3","type2":"value4"} 

and want data draw table inside html, using ajax:

function showtable() {      $.ajax('../json', {         method: 'get',         success: function (users) {             var result = "<tr>" +                 "<th>type1</th>" +                 "<th>type2</th>" +                 "</tr>";                 (var = 0; < users.length; i++) {                 result += "<tr>" +                     "<td>"+users[i].type1+"</td>" +                     "<td>"+users[i].type2+"</td>" +                     "</tr>";             }             var table = document.getelementbyid("showtable");             table.innerhtml(result);         }     }); } 

but nothing happens.

servlet:

public class jsoncontroller extends httpservlet {     @override     protected void doget(httpservletrequest req, httpservletresponse resp) throws servletexception, ioexception {         resp.setcontenttype("text/json");         printwriter writer = new printwriter(resp.getoutputstream());         (object j : dbcontroller.getcontroller().getusersinjson()) {             writer.append(j.tostring());             writer.flush();         }     } 

this how getusersinjson() looks like. takes data arraylist<user> array , returns jsonarray :

public jsonarray getusersinjson() {         jsonarray array = new jsonarray();         (user user : dbcontroller.getcontroller().get()) {             jsonobject juser = new jsonobject();             juser.put("name", user.getname());             juser.put("login", user.getlogin());             juser.put("email", user.getemail());             juser.put("role", user.getrole().getname());             juser.put("country", user.getcountry());             juser.put("city", user.getcity());             array.put(juser);         }         return array;     } 

that going wrong?

you doing wrong, have included variable in quotes value not substituted. way:

$.ajax('../json', {         method: 'get',         success: function (users) {             var result = "<tr>" +                 "<th>type1</th>" +                 "<th>type2</th>" +                 "</tr>";                 (var = 0; < users.length; i++) {                 result += "<tr>" +                     "<td>"+users[i].type1+"</td>" +                     "<td>"+users[i].type2+"</td>" +                     "</tr>";             }             var table = document.getelementbyid("showtable");             table.innerhtml(result);         }     }); 

Comments

Popular posts from this blog

networking - Vagrant-provisioned VirtualBox VM is not reachable from Ubuntu host -

c# - ASP.NET Core - There is already an object named 'AspNetRoles' in the database -

ruby on rails - ArgumentError: Missing host to link to! Please provide the :host parameter, set default_url_options[:host], or set :only_path to true -