【发布时间】:2018-07-06 05:45:30
【问题描述】:
我正在尝试将 JSON 数据从 servlet 发送到我的 JSP 页面。 一旦我运行 JSP,整个 JSP 就会被 JSON 内容覆盖。为什么会这样?这是我的 JSP 页面的一部分。
<%@ page import="java.util.ArrayList" %>
<head>
<script>
function populateTypeOfWork(workType) {
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function () {
if (this.readyState == 4 && this.status == 200) {
var work = JSON.parse(this.responseText);
workType.options.length = 0;
for (i = 0; i < work.length; i++) {
createOption(workType, workDesc[i], workDesc[i]);
}
}
xhttp.open("GET", "ServletRequest", true);
xhttp.send();
}
}
function createOption(dropdown, text, value) {
var x = document.createElement('option');
x.value = value;
x.text = text;
dropdown.options.add(x);
}
</script>
</head>
<body>
<label>Work Category **</label>
<select id="workCategory"
onchange="populateTypeOfWork(document.getElementById('workTypeDesc1'))"
size=1
style="width:95%;" required>
<option value="" selected>Select A Work Category</option>
<% ArrayList workCategory = (ArrayList)
request.getAttribute("workCategory");
for (int i = 0; i < workCategory.size(); i++) {
%>
<option value="<%=workCategory.get(i)%>"><%=workCategory.get(i)%>
</option>
<% } %>
</select> <br>
<label>Type Of Work **</label>
<select id="workTypeDesc1" size=1 style="width:95%;" required>
<option value="" selected>Type of Work</option>
</select>
</body>
</html>
小服务程序
String json = new Gson().toJson(arraylistvar);
res.setContentType("application/json");
PrintWriter pw = res.getWriter();
pw.flush();
pw.close();
req.setAttribute("viewURL","/page1.jsp");
为什么 stackoverflow 一直要求我添加更多详细信息?因为它主要是代码..??我为什么要不必要地填写这个帖子?
【问题讨论】: