4

Retrieve data from an arraylist in the ajax success function. I need to populate my textfields latitude and longitude fields based on the ID passed via ajax. Everything works fine but the data is not rendered to the text fields. The success function returns nothing if the below 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"/>
6
  • Any error on firebug console? And you see that request can be sent on network tab of firebug? Commented Feb 25, 2014 at 7:01
  • Sir no errors and on the console its printingg the values fine. Its just not retrieving in the success function. Commented Feb 25, 2014 at 7:03
  • Then, your ajax request is not functioning well. Check Network tab of firebug. What are you getting on ajax request Commented Feb 25, 2014 at 7:04
  • I am getting as undefined sir. data is not returning any value Commented Feb 25, 2014 at 7:06
  • Could you please go to ..../Servleturl?dataID=some_value on your web browser? some_value needs to be available value. I want to know exact output of your controller request Commented Feb 25, 2014 at 7:08

3 Answers 3

5

Thank you for all the responses.

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

I did 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 a arraylist should give the data as array itself to retrieve the values..

Thank you all for the answers.

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

Comments

0

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

`

1 Comment

It is not returning any values first of all sir.
0

You need to return jsonArray.toString(). If you don't want to use that you need to put extra code;

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())

}

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.