我正在尝试使用sendxmlRequest()方法基于eqid从表中获取值:
<select name="eqNAME" onchange="sendxmlRequest('GET','getEquipDetails.jsp',this.value)
<options> ..... <options>
</select>
这是我在ajax.js文件中添加的
//Make the XMLHttpRequest Object
var http = createRequestObject();
function sendxmlRequest(method, url,eqid){
url = url + "?eqid="+eqid;
if(method == 'get' || method == 'GET'){
http.open(method,url,true);
http.onreadystatechange = handleResponse;
http.send(null);
}
}
function createRequestObject(){
var req; try {
// Firefox, Opera, Safari
req = new XMLHttpRequest();
} catch (e) {
// Internet Explorer
try {
//For IE 6
req = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try {
//For IE 5
req = new ActiveXObject("Microsoft.XMLHTTP");
}
catch (e) {
alert('Your browser is not IE 5 or higher, or Firefox or Safari or Opera');
}
}
}
return req;
}
以下用于处理响应(在ajax.js中):
function handleResponse(){
if(http.readyState == 4 && http.status == 200){
var response = http.responseText;
}
}
这是我的'getEquipDetails.jsp'文件:
<%
String planLoc= theResult1.getString(2) == null ? "":theResult1.getString(3);
String changLoc= theResult1.getString(3) == null ? "":theResult1.getString(4)
%>
<%
response.setHeader("Pragma", "no-cache"); //HTTP 1.0
response.setDateHeader("Expires", 0); //prevents caching at the proxy server
response.setHeader("Cache-Control", "no-cache, private, no-store, max-stale=0"); // HTTP 1.1
%>
我的查询是,我们如何从getEquipDetails.jsp获取值plansLoc和changLoc并将其设置在responseText中,以便可以在我的页面下拉列表中更新它?
或者还有其他方法吗?
注意:我没有给出表检索代码,因为已经处理过了。我只想在我的JSP页面中更新planLoc和changLoc
答案 0 :(得分:1)
在getEquipDetails.jsp
写入值response.getWriter()
对象。
<%
response.setHeader("Pragma", "no-cache"); //HTTP 1.0
response.setDateHeader("Expires", 0); //prevents caching at the proxy server
response.setHeader("Cache-Control", "no-cache, private, no-store, max-stale=0"); // HTTP 1.1
PrintWriter res = response.getWriter();
res.println(planLoc);
res.println(changLoc);
res.close();
%>
然后在
function handleResponse(){
if(http.readyState == 4 && http.status == 200){
document.getElementById("dataTable").innerHTML = http.responseText;
// dataTable will be id of any HTML tag where you want to display the updated value.
}
}
例如:
<div id="dataTable"></div>
因此,AJAX的响应将在此处设置。