Jquery 将值从arraylist返回到ajax成功函数

Jquery 将值从arraylist返回到ajax成功函数,jquery,ajax,jsp,servlets,arraylist,Jquery,Ajax,Jsp,Servlets,Arraylist,在ajax成功函数中从arraylist检索数据。我需要根据通过ajax传递的ID填充文本字段纬度和经度字段。一切正常,但数据不会呈现到文本字段。如果执行以下代码,success函数将不返回任何内容。我的代码中写了什么 FetchData.class public static ArrayList<Info> getAllInfo(String data_id) { connection = FetchData.getConnection(); ArrayList<Info&g

在ajax成功函数中从arraylist检索数据。我需要根据通过ajax传递的ID填充文本字段纬度和经度字段。一切正常,但数据不会呈现到文本字段。如果执行以下代码,success函数将不返回任何内容。我的代码中写了什么

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;
  }
}
index.jsp

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

输入字段值
`


`

您需要返回
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())

}
受保护的void doGet(HttpServletRequest请求,HttpServletResponse响应)抛出ServletException,IOException{
字符串dataID=request.getParameter(“数据_id”);
ArrayList in=新的ArrayList();
in=FetchData.getAllInfo();
Gson Gson=新的Gson();
JsonElement元素=gson.toJsonTree(在中,newtypetoken(){}.getType());
setContentType(“application/json;charset=UTF-8”);
PrintWriter printOut=response.getWriter();
JsonArray JsonArray=element.getAsJsonArray();
打印输出。打印(jsonArray);
printout.flush();
//或
//printOut.print(jsonArray.toString())
}

感谢您的回复

我终于找到了解决我自己问题的办法。希望对其他人有用

我执行了以下代码

$.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;
    }
});
由于数据是从arraylist返回的,所以它应该将数据作为数组本身来获取值


谢谢大家的回答

首先它没有返回任何值,长官。firebug控制台有错误吗?您可以在firebug的网络选项卡上发送请求吗?先生,没有错误,在控制台上打印的值很好。它只是没有在success函数中检索。那么,您的ajax请求就不能正常运行。检查firebug的网络选项卡。你说的是什么?我说的是没有定义的先生。数据未返回任何值请转到web浏览器上的
…/Servleturl?dataID=some\u value
<代码>某些值
需要是可用值。我想知道控制器请求的确切输出
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())

}
$.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;
    }
});