【问题标题】:Return values from arraylist to an ajax success function从arraylist返回值到ajax成功函数
【发布时间】:2014-03-27 05:23:07
【问题描述】:

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

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 类

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

}

我的 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"/>

【问题讨论】:

  • 萤火虫控制台有任何错误吗?你看到可以在firebug的网络选项卡上发送请求了吗?
  • 先生没有错误,在控制台上打印的值很好。它只是没有在成功函数中检索。
  • 那么,您的 ajax 请求运行不正常。检查萤火虫的网络选项卡。你在 ajax 请求上得到了什么
  • 我越来越不确定先生。数据没有返回任何值
  • 您能在您的网络浏览器上转到..../Servleturl?dataID=some_value 吗? some_value 需要是可用值。我想知道您的控制器请求的确切输出

标签: jquery ajax jsp servlets arraylist


【解决方案1】:

输入输入字段值 &lt;input type="text" id="data_id" onblur=""/&gt; &lt;input type="text" id="lat" value=""/&gt; `

`

【讨论】:

  • 先生,它首先没有返回任何值。
【解决方案2】:

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

}

【讨论】:

    【解决方案3】:

    感谢您的所有回复。

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

    我做了以下代码。

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

    由于数据是从数组列表返回的,因此应该将数据作为数组本身来检索值..

    谢谢大家的回答。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-03-10
      • 2015-06-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多