gpt4 book ai didi

java - 使用 ajax 从 servlet 填充下拉列表

转载 作者:行者123 更新时间:2023-12-01 14:35:32 25 4
gpt4 key购买 nike

我需要使用ajax从数据库填充一个下拉列表,我使用两个下拉列表,如果选择第一个下拉列表值,则第二个下拉列表值(必须根据第一个下拉列表中选择的值从数据库检索) ) 必须显示。 DAO(数据访问层)返回 4 个结果作为 arraylist 对象,但在 http 响应文本中,它作为对象打印而不是值。我尝试使用 for 循环来迭代它,但我无法实现它。请在这方面帮助我。

HTML 代码:

    // First Drop Down
Question Field :<select name="ddlAddQuestionField" id='ddlAddQuestionField' onchange="getFieldPosition()">
<option value=''>Select Question Field</option>
<option value='Security Question'>Security Question</option>
<option value='Personal Info'>Personal Info</option>
</select>

// Second DropDown

User Field Position:<select name="userFieldPosition" id="userFieldPosition" disabled="disabled"> </select>

Javascript代码

function getFieldPosition(){

var fieldName =$("#ddlAddQuestionField").val();

if(window.XMLHttpRequest){
xmlhttp=new XMLHttpRequest();
} else {
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function() {

if(xmlhttp.readyState==4 && xmlhttp.status==200){

document.getElementById("userFieldPosition").disabled=false;
alert(xmlhttp.responseText);
var response =xmlhttp.responseText;

for(var i=0;i<response.length;i++) {
var elements = "<option value='"+response[i]+"'>"+response[i]+"</option>";
$("#userFieldPosition").append(elements);
}
}
}
xmlhttp.open("POST","ApplicationController",true);
xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
xmlhttp.send("fieldAction=fieldPosition&fieldName="+fieldName);
}

Servlet 代码

fieldPositionObj = fieldPositionDaoObj.getFieldPosition(fieldName);   //Hitting the Dao

// In dao it returns arraylist object.

response.setContentType("text/plain");
response.setCharacterEncoding("UTF-8");
response.getWriter().write(fieldPositionDaoObj);

最佳答案

你的问题是线路

 response.getWriter().write(fieldPositionDaoObj);

您正在直接在响应上编写java Obejct..我认为fieldPositionDaoObj是列表或aray..所以toString表示是

(com.bean.QuestionInfoBean@23d275, com.bean.QuestionInfoBean@1ce15f6, com.bean.QuestionInfoBean@103fcaa, com.bean.QuestionInfoBean@c135d6) 

其中 com.bean.QuestionInfoBean@c135d6 是 Java 对象的 toString 表示形式。

我认为你需要返回 Java 对象/列表/数组的 JSON 表示,你的代码就可以工作

JSON (JavaScript Object Notation), is a simple and easy to read and write data exchange format. It’s popular and implemented in countless projects worldwide, for those don’t like XML, JSON is a very good alternative solution.

您的 JSON 表示形式应类似于

[ First,Middle,Full,Last]

[{First},{Middle},{Full},{Last}]

您可以编写自己的方法,例如 public String getJSOnRepresentation();

然后执行

response.getWriter().write(fieldPositionDaoObj.getJSOnRepresentation());

<强> Sample example on What and how to code

另请参阅

http://json.org/java/ https://github.com/douglascrockford/JSON-java/blob/master/JSONString.java https://github.com/douglascrockford/JSON-java/blob/master/JSONArray.java https://github.com/douglascrockford/JSON-java/blob/master/JSONObject.java

关于java - 使用 ajax 从 servlet 填充下拉列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16520346/

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