Returns values ​​from arraylist to ajax success function

Get data from arraylist in ajax success function. I need to fill in textbox fields with latitude and longitude based on the identifier passed through ajax. Everything works fine, but the data does not appear in the text fields. The success function returns nothing if the following code is executed. What is wroong in my code?

Fetchdata.class

public static ArrayList<Info> getAllInfo(String data_id) {
connection = FetchData.getConnection();
ArrayList<Info> inf = new ArrayList<Info>();
try {
    Statement statement = connection.createStatement();
    ResultSet rs = statement.executeQuery("select * from info_table where data_id='"+data_id"'");

    while(rs.next()) {  
        Info in=new Info();
        in.setData_id(rs.getString("data_id"));
        in.setLat(rs.getDouble("Lat"));
        in.setLongi(rs.getDouble("Longi"));
        inf.add(in);
    }
} catch (SQLException e) {
    e.printStackTrace();
}

return inf;
  }
}

Servlet class

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
 String dataID=request.getParameter("data_id");
ArrayList<Info> in=new ArrayList<Info>();
in=FetchData.getAllInfo();
Gson gson = new Gson();
JsonElement element = gson.toJsonTree(in, new TypeToken<List<Info>>() {}.getType());

JsonArray jsonArray = element.getAsJsonArray();
response.setContentType("application/json");
response.getWriter().print(jsonArray);

}

my ajax

$.ajax({
url:'Servleturl?dataID='document.getElementById("#data_id").value;
type:'GET',
dataType:'json',
success:function(data){
$("#lat").val(data.Lat);
$("#longi").val(data.Longi);
}
});
});

index.jsp

<input type="text" id="data_id" onblur=""/>
<input type="text" id="lat"/>
<input type="text" id="longi"/>
+3
source share
3 answers

Thanks for all the answers.

I finally found a solution to my question. hope it will be useful for someone else.

I made the following code.

$.ajax({
    url:'Servleturl?dataID='document.getElementById("#data_id").value;
    type:'GET',
    dataType:'json',
    success:function(data) {
        document.getElementById("#lat").value=data[0].Lat;
        document.getElementById("#longi").value=data[0].Longi;
    }
});

As the data is returned from the arraylist, they must provide the data as an array to receive the values.

.

+4

<input type="text" id="data_id" onblur=""/> <input type="text" id="lat" value=""/> `

`

0

jsonArray.toString(). , ;

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    String dataID=request.getParameter("data_id");
    ArrayList<Info> in=new ArrayList<Info>();
    in=FetchData.getAllInfo();
    Gson gson = new Gson();
    JsonElement element = gson.toJsonTree(in, new TypeToken<List<Info>>() {}.getType());

    response.setContentType("application/json; charset=UTF-8");
    PrintWriter printOut = response.getWriter();

    JsonArray jsonArray = element.getAsJsonArray();
    printOut.print(jsonArray);
    printout.flush();
    // Or
    // printOut.print(jsonArray.toString())

}
0
source

All Articles