gpt4 book ai didi

javascript - 通过 ajax 函数调用返回下拉列表

转载 作者:行者123 更新时间:2023-11-29 23:41:26 25 4
gpt4 key购买 nike

我正在尝试通过 ajax 函数调用返回一个数组列表,并将结果返回到我的 HTML 下拉列表中。
下面是我的 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());

}

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

HTML代码:

    <div class="cell">
Select Session for Student
<div class="input-control select mutiple full-size">
<select id="sessid" name="sessid">
</select>

</div>
</div>

最佳答案

服务器期待一个POST参数"studentid"

Integer p = Integer.parseInt(request.getParameter("studentid"));

虽然"name"通过了。替代

url : "<%=context%>/ListSessionsServlet?studentid=" + name

对于

url : "<%=context%>/ListSessionsServlet?name=" + name

关于javascript - 通过 ajax 函数调用返回下拉列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45135250/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com