Javascript 通过ajax函数调用返回下拉列表
我试图通过ajax函数调用返回arraylist,并将结果返回到我的HTML下拉列表中。Javascript 通过ajax函数调用返回下拉列表,javascript,java,servlets,Javascript,Java,Servlets,我试图通过ajax函数调用返回arraylist,并将结果返回到我的HTML下拉列表中。 下面是我的ajax函数、servlet代码和html。 我的下拉列表总是空的。不知道哪里出了问题 Servlet代码: protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { Integer p = Integer.p
下面是我的ajax函数、servlet代码和html。
我的下拉列表总是空的。不知道哪里出了问题 Servlet代码:
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
Integer p = Integer.parseInt(request.getParameter("studentid"));
ArrayList<SessionDataBean> result = new ArrayList<>();
String sessid = null;
try ( Connection con = JdbcUtil.getConnection()) {
String sql= "select distinct F.SESSIONID "
+ "from Students F "
+ "where F.studentid = "+p;
try (Statement st = con.createStatement()) {
System.out.println(sql);
ResultSet rs = st.executeQuery(sql);
while (rs.next()){
result.add(new SessionDataBean(rs.getString(1)));
}
} catch (Exception e) {
e.printStackTrace();
}
}
catch (Exception e) {
e.printStackTrace();
}
response.setContentType("application/json");
new Gson().toJson(result, response.getWriter());
}
protectedvoiddopost(HttpServletRequest请求,HttpServletResponse响应)抛出ServletException,IOException{
整数p=Integer.parseInt(request.getParameter(“studentid”);
ArrayList结果=新建ArrayList();
字符串sessiond=null;
try(Connection con=JdbcUtil.getConnection()){
String sql=“选择不同的F.SESSIONID”
+“来自学生F”
+“其中F.studentid=“+p;
try(语句st=con.createStatement()){
System.out.println(sql);
结果集rs=st.executeQuery(sql);
while(rs.next()){
add(新的SessionDataBean(rs.getString(1));
}
}捕获(例外e){
e、 printStackTrace();
}
}
捕获(例外e){
e、 printStackTrace();
}
setContentType(“应用程序/json”);
新的Gson().toJson(result,response.getWriter());
}
Ajax函数调用代码:
function listSession(){
var name = document.getElementById("studentid").value;
$.ajax({
url : "<%=context%>/ListSessionsServlet?studentid=" + name,
type : "POST",
async : false,
dataType: "json",
success : function(data) {
var toAppend = '';
$.each(data,function(i,o){
toAppend += '<option>'+o.id+'</option>'; <---issue might be here?
});
$('#sessid').append(toAppend);
}
});
}
函数listSession(){
var name=document.getElementById(“studentid”).value;
$.ajax({
url:“/ListSessionServlet?studentid=“+name,
类型:“POST”,
async:false,
数据类型:“json”,
成功:功能(数据){
var toAppend='';
$。每个(数据、函数(i、o){
toAppend+=''+o.id+'';服务器需要一个POST
参数“studentid”
位于
Integer p=Integer.parseInt(request.getParameter(“studentid”);
虽然传递了“name”
。替换
url:“/ListSessionServlet?studentid=“+name
为了
url:“/ListSessionServlet?name=“+name
不确定哪里出了问题。这就是调试的目的。您的ajax调用当前无法运行。追加看起来足够好。请尝试添加错误:函数(XMLHttpRequest,textStatus,errorSprown){警报(“状态:+textStatus”);警报(“错误:”+errorshown);}
到您的ajax调用。同时,在成功下拉列表中检查空数据仍然为空。@DED865在success
处的参数是list
,尽管您将未定义的数据传递到$。每个()
?如果“+o.id+”中的o.id是其他东西吗?@guest271314是的,我纠正了这个问题…现在这两个区域都是“数据”。我让它工作了。必须将GSON jar文件放在WEB-INF/lib文件夹中。天哪。
<div class="cell">
Select Session for Student
<div class="input-control select mutiple full-size">
<select id="sessid" name="sessid">
</select>
</div>
</div>